Rust 实例 —— 日期时间

2022-02-15  本文已影响0人  1UP2020

打印当前和任意日期时间

use chrono::{DateTime, Local, Datelike, NaiveDate};


fn main() {
    //打印当前日期时间
    let now: DateTime<Local> = Local::now();
    let w = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"];
    println!("今天是{}{} {}",
    now.format("%Y年%m月%e日"), 
    w[now.weekday().num_days_from_monday() as usize],
    now.format("%T")
    );
    //打印任意日期
    let date: NaiveDate = NaiveDate::from_ymd(2022, 2, 14);
    let (_, year) = date.year_ce();
    println!("{}年{}月{}日{}",
        year,
        date.month(),
        date.day(), 
        w[date.weekday().num_days_from_monday() as usize]
    ); 
}

上一篇下一篇

猜你喜欢

热点阅读