【Rust快速入门】-- 时间转换

rust时间转换

  • 依赖
# doc: https://crates.io/crates/chrono
[dependencies]
chrono = "0.4.19"
  • 示例
use chrono::prelude::*;

pub fn test_chrono() {
    // 获取本地时间 doc: https://crates.io/crates/chrono
    let now: DateTime<Local> = Local::now();
    println!("{:?}", now);
    // 输出格式化
    let fmt = "%Y-%m-%d %H:%M:%S";
    let dft = now.format(fmt);
    println!("{:?}", dft.to_string());
    // 转时间戳
    println!("{:?}", now.timestamp_millis());
    // 字符串转时间对象
    let t = NaiveDateTime::parse_from_str("2022-02-10 00:00:00", fmt).unwrap();
    println!("{:?}", t);
    println!("{:?}", t.date());
}

你可能感兴趣的:(rust)