How do I add days to a Chrono UTC?

…衆ロ難τιáo~ 提交于 2020-12-04 15:43:20

问题


I am trying to find the preferred way to add days to a Chrono UTC. I want to add 137 days to the current time:

let dt = UTC::now();

回答1:


Just use Duration and appropriate operator:

use chrono::{Duration, Utc};

fn main() {
    let dt = Utc::now() + Duration::days(137);

    println!("today date + 137 days {}", dt);
}

Test on playground.




回答2:


I just wanted to improve on @Stargateur answer. There is no need to use time crate, since chrono crate has Duration struct in it:

extern crate chrono;

use chrono::{Duration, Utc};

fn main() {
    let dt = Utc::now() + Duration::days(137);

    println!("{}", dt);
}

Another test on playground



来源:https://stackoverflow.com/questions/44710877/how-do-i-add-days-to-a-chrono-utc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!