美文网首页Rust 学习笔记
rust - 时间使用笔记

rust - 时间使用笔记

作者: 国服最坑开发 | 来源:发表于2020-03-06 19:26 被阅读0次

    今天在写接口的时候 ,用到了时间处理, 相关内容记下, 以备后用.

    • 前置准备
    Cargo.toml
    # time
    chrono = { version = "0.4", features = ["serde"] }
    
    # 引入
    use chrono::{DateTime, Local, NaiveDateTime, Duration};
    
    
    • 当前系统时间
       let now:NaiveDateTime = Local::now().naive_local();
    
    • 从String转换
     let end_time = NaiveDateTime::parse_from_str("2020-03-06 18:36:27", "%Y-%m-%d %H:%M:%S").unwrap_or(now);
    
    • 时间增减操作
       // 减 9 分钟 
       let start_time = end_time.sub(Duration::minutes(9));
    
    • 时间按格式输出String
        let start_day = start_time.format("%Y%m%d").to_string();
    

    相关文章

      网友评论

        本文标题:rust - 时间使用笔记

        本文链接:https://www.haomeiwen.com/subject/ervgrhtx.html