Search code examples
datetimerustsecondsrust-chrono

How can I use the Chrono library to get seconds since Unix epoch?


I can get the date, the hours, minutes, seconds and nanoseconds in the date format, but I can't get seconds as a floating point or integer number.

extern crate chrono;
use chrono::prelude::*;
fn main() {
    let local: DateTime<Local> = Local::now();
    println!("{}", local); 
} 

I have already read the docs.


Solution

  • I solved the problem using the function timestamp as written in the docs. It didn't work before because I forgot to use the brackets to call timestamp.

    extern crate chrono;
    
    use chrono::prelude::*;
    
    fn main() {
        let local: DateTime<Local> = Local::now();
        println!("{}", local.timestamp()); // I forgot the brackets after timestamp
    }
    

    Thanks to mcarton.