Expand description
This crate provides helper functions for calculating shifts in Chrono’s NaiveDate values for various periods (week, month, quarter, year) for common shifts in direction (beginning_of_, end_of_, previous_, and next_).
The dates passed to these functions should be Gregorian dates to ensure proper calcuation.
use chrono::prelude::*;
use date_calculations::*;
let twenty_twenty_one = NaiveDate::from_ymd_opt(2021, 1, 31).unwrap();
assert_eq!(next_year(&twenty_twenty_one).unwrap().year(), 2022);
assert_eq!(next_year(&twenty_twenty_one).unwrap().month(), 1);
assert_eq!(next_year(&twenty_twenty_one).unwrap().day(), 1);
assert_eq!(previous_quarter(&twenty_twenty_one).unwrap().year(), 2020);
assert_eq!(previous_quarter(&twenty_twenty_one).unwrap().month(), 10);
assert_eq!(previous_quarter(&twenty_twenty_one).unwrap().day(), 1);
Functions§
- beginning_
of_ month - Returns the first day of the current month and year.
- beginning_
of_ quarter - Returns the first day of the current quarter and year.
- beginning_
of_ week - Returns the beginning of the week relative to the provided date.
- beginning_
of_ year - Returns the first day of the year (January 1) of the current year.
- end_
of_ month - Returns the last day of the current month and year.
- end_
of_ quarter - Returns the last day of the current quarter and year.
- end_
of_ week - Returns the end of the week relative to the provided date.
- end_
of_ year - Returns the last day of the year (December 31) of the current year.
- next_
month - Returns the first day of the next month.
- next_
quarter - Returns the first day of the next quarter.
- next_
week - Returns the beginning of the next week.
- next_
year - Returns the first day of the year (January 1) of the next year.
- previous_
month - Returns the first day of the previous month.
- previous_
quarter - Returns the first day of the previous quarter.
- previous_
week - Returns the end of the next week.
- previous_
year - Returns the first day of the year (January 1) of the previous year.