首页 文章

如何用毫秒解析日期?

提问于
浏览
7

我有以下格式的日期: "2014-03-10 11:20:34.3454" . 我该如何解析这个日期?

chrono doc提到解析年,月,......,分钟和秒 . 没有毫秒 . 当我再次看_818838时 - 没有毫秒 .

另一方面,我可以像_818840这样创建一个 DateTime . 所以Rust知道几毫秒......

1 回答

  • 5
    extern crate time;
    
    fn main() {
        match time::strptime("2014-03-10 11:20:34.3454",
                             "%Y-%m-%d %H:%M:%S.%f")
        {
            Ok(v) => println!("{}", time::strftime("%Y/%m/%d %H:%M:%S.%f",
                                                   &v).unwrap()),
            Err(e) => println!("Error: {}", e),
        };
    }
    

    输出:

    2014/03/10 11:20:34.345400000
    

    strptime()strftime() 在使用时间值时非常有用 . 此外,他们倾向于使用大多数语言,所以学习它一旦付出很好的代价 .

相关问题