Skip to content

Latest commit

 

History

History
26 lines (20 loc) · 1.2 KB

format.zh.md

File metadata and controls

26 lines (20 loc) · 1.2 KB

显示格式化的日期和时间

[![chrono-badge]][chrono] [![cat-date-and-time-badge]][cat-date-and-time]

使用Utc::now,获取并显示当前时间(以 UTC 为单位)。以众所周知的格式RFC 2822,格式化当前时间,通过DateTime::to_rfc2822。还有RFC 3339格式,可以使用DateTime::to_rfc3339,除此之外,用DateTime::format可以自定义格式。

extern crate chrono;
use chrono::{DateTime, Utc};

fn main() {
    let now: DateTime<Utc> = Utc::now();

    println!("UTC now is: {}", now);
    println!("UTC now in RFC 2822 is: {}", now.to_rfc2822());
    println!("UTC now in RFC 3339 is: {}", now.to_rfc3339());
    println!("UTC now in a custom format is: {}", now.format("%a %b %e %T %Y"));
}