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
- How to match whitespace with a regex in Rust?
- How to match a URL with a regex in Rust?
- How to replace all matches using Rust regex?
- Yield example in Rust
- How to use regex lookbehind in Rust?
- How to replace strings using Rust regex?
- Regex example to match multiline string in Rust?
- How to use captures_iter with regex in Rust?
- How to compare two HashSets in Rust?
- How to use regex lookahead in Rust?
See more codes...