如何用毫秒解析日期? [英] How to parse a date with milliseconds?

查看:73
本文介绍了如何用毫秒解析日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

chrono doc 提到了解析年、月、...、分和秒.没有毫秒.同样,当我再次查看 rust-datetime 时 - 没有毫秒.

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

另一方面,我可以像这样创建 DateTime UTC.ymd(2014, 11, 28).and_hms_milli(7, 8, 9, 10).所以 Rust 知道毫秒...

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...

推荐答案

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() 在使用时间值时非常有用.此外,他们往往会使用大多数语言,因此一旦学习它,就会随着时间的推移获得丰厚的回报.

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.

这篇关于如何用毫秒解析日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆