Skip to main content

add_date_time_diff

Function add_date_time_diff 

Source
pub fn add_date_time_diff<DT: AddDateTimeDiff>(
    from: DT,
    date_time_diff: &impl DateTimeDiff,
) -> DT::Output
Expand description

Calculate from + date_time_diff.

Years and months are added first, and the day is changed to the last day of the month if that month is shorter. Then days, hours, minutes, seconds and nanoseconds are added in this order. See AddDateTimeDiff for the output type.

ยงExample

use chrono::prelude::*;
use date_differencer::{DateDiffResult, add_date_time_diff};

let date = Local.with_ymd_and_hms(2000, 1, 1, 0, 0, 0).unwrap();

let date_after_1_year_1_day = add_date_time_diff(date, &DateDiffResult {
    years: 1,
    days: 1,
    ..DateDiffResult::default()
})
.unwrap();

assert_eq!(
    Local.with_ymd_and_hms(2001, 1, 2, 0, 0, 0).unwrap(),
    date_after_1_year_1_day
)