How to parse a date with milliseconds?

℡╲_俬逩灬. 提交于 2021-01-27 04:25:27

问题


I have a date in the following format: "2014-03-10 11:20:34.3454". How can I parse this date?

The chrono doc mentions parsing year, month, ..., minutes and seconds. No milliseconds. Also when I look at rust-datetime again - no milliseconds.

On the other hand, I can create a DateTime like this UTC.ymd(2014, 11, 28).and_hms_milli(7, 8, 9, 10). So Rust knows milliseconds...


回答1:


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),
    };
}

Output:

2014/03/10 11:20:34.345400000

strptime() and strftime() are quite helpful when using time values. Plus, they tend to work in most languages so learning it once pays well over time.



来源:https://stackoverflow.com/questions/31126366/how-to-parse-a-date-with-milliseconds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!