简体   繁体   English

如何使用 Chrono 库获取秒数?

[英]How can I use the Chrono library to get seconds?

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.我已经阅读了文档。

Use Timelike::second :使用Timelike::second

extern crate chrono;

use chrono::prelude::*;

fn main() {
    let local: DateTime<Local> = Local::now();
    println!("{}", local.second()); 
} 

I solved the problem using the function timestamp as written in the docs.我使用文档中写的函数timestamp解决了这个问题。 It didn't work before because I forgot to use the brackets to call timestamp .之前它不起作用,因为我忘记使用括号来调用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 .感谢麦卡顿

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM