How to convert time in milliseconds to number of years in Rust

1 Answer

0 votes
use std::time::{UNIX_EPOCH};

fn main() {
    let millis = 1476907455894;
    let duration = std::time::Duration::from_millis(millis);
    let time = UNIX_EPOCH + duration;

    let datetime = time.duration_since(UNIX_EPOCH).unwrap();
    let total_years = datetime.as_secs() / (365 * 24 * 60 * 60);
    println!("{}", total_years);

    let year = 1970 + (total_years as i64);
    println!("{}", year);
}

  
    
/*
run:
  
46
2016
  
*/

 



answered Oct 5, 2024 by avibootz

Related questions

...