expression

Chrono checked calendar-day addition

Use calendar days for date-only tasks such as a delivery date. Checked arithmetic returns None if the result is outside the supported date range. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): 2024-03-01 Common mistake: Manually adding days to a day-of-month field.

Formal Syntax & Structure

rust syntaxrust
date.checked_add_days(Days::new(n)) -> Option<NaiveDate>

Working Example

Example Snippetrust
fn main() {    use chrono::{Days, NaiveDate};    let start = NaiveDate::from_ymd_opt(2024, 2, 28).unwrap();    println!("{}", start.checked_add_days(Days::new(2)).unwrap());}
Rust Reference