rustHow to add day to date in Rust
Adding a day to a date in Rust can be done using the add_days method from the chrono crate. This method takes a NaiveDate and an i64 as parameters and returns a new NaiveDate with the added days.
Example code
use chrono::NaiveDate;
let date = NaiveDate::from_ymd(2020, 1, 1);
let new_date = date.add_days(1);
println!("{}", new_date);
Output example
2020-01-02
Code explanation
use chrono::NaiveDate: imports theNaiveDatetype from thechronocrate.let date = NaiveDate::from_ymd(2020, 1, 1): creates aNaiveDatefrom the given year, month and day.let new_date = date.add_days(1): adds one day to the givenNaiveDate.println!("{}", new_date): prints the newNaiveDateto the console.
Helpful links
Related
More of Rust
- Regex example to match multiline string in Rust?
- How to map a Rust slice?
- How to push an element to a Rust slice?
- How to match whitespace with a regex in Rust?
- How to replace strings using Rust regex?
- How to create a HashMap of structs in Rust?
- How to sort a Rust HashMap?
- How to convert a u8 slice to a hex string in Rust?
- How to compile a regex in Rust?
- How to use regex captures in Rust?
See more codes...