expression
Chrono month addition and clamping
Calendar months have variable lengths. This method uses the last valid day when the original day does not exist in the destination month. Repeated monthly additions may drift from an original end-of-month schedule. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): 2024-02-29 2024-03-29 Common mistake: Assuming every month lasts 30 days or that repeated additions preserve the last day of the month.
Formal Syntax & Structure
rust syntaxrust
date.checked_add_months(Months::new(n)) -> Option<NaiveDate>Working Example
Example Snippetrust
fn main() { use chrono::{Months, NaiveDate}; let start = NaiveDate::from_ymd_opt(2024, 1, 31).unwrap(); let feb = start.checked_add_months(Months::new(1)).unwrap(); let mar = feb.checked_add_months(Months::new(1)).unwrap(); println!("{feb} {mar}");}