Trait Datelike
trait Datelike: Sized
The common set of methods for date component.
Methods such as year, month, day and weekday can be used to get basic
information about the date.
The with_* methods can change the date.
Warning
The with_* methods can be convenient to change a single component of a date, but they must be
used with some care. Examples to watch out for:
with_yearchanges the year component of a year-month-day value. Don't use this method if you want the ordinal to stay the same after changing the year, of if you want the week and weekday values to stay the same.- Don't combine two
with_*methods to change two components of the date. For example to change both the year and month components of a date. This could fail because an intermediate value does not exist, while the final date would be valid.
For more complex changes to a date, it is best to use the methods on NaiveDate to create a
new value instead of altering an existing date.
Required Methods
fn year(self: &Self) -> i32Returns the year number in the calendar date.
fn month(self: &Self) -> u32Returns the month number starting from 1.
The return value ranges from 1 to 12.
fn month0(self: &Self) -> u32Returns the month number starting from 0.
The return value ranges from 0 to 11.
fn day(self: &Self) -> u32Returns the day of month starting from 1.
The return value ranges from 1 to 31. (The last day of month differs by months.)
fn day0(self: &Self) -> u32Returns the day of month starting from 0.
The return value ranges from 0 to 30. (The last day of month differs by months.)
fn ordinal(self: &Self) -> u32Returns the day of year starting from 1.
The return value ranges from 1 to 366. (The last day of year differs by years.)
fn ordinal0(self: &Self) -> u32Returns the day of year starting from 0.
The return value ranges from 0 to 365. (The last day of year differs by years.)
fn weekday(self: &Self) -> WeekdayReturns the day of week.
fn iso_week(self: &Self) -> IsoWeekReturns the ISO week.
fn with_year(self: &Self, year: i32) -> Option<Self>Makes a new value with the year number changed, while keeping the same month and day.
This method assumes you want to work on the date as a year-month-day value. Don't use it if you want the ordinal to stay the same after changing the year, of if you want the week and weekday values to stay the same.
Errors
Returns
Nonewhen:- The resulting date does not exist (February 29 in a non-leap year).
- The year is out of range for
NaiveDate. - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time.
Examples
use ; assert_eq!; // Resulting date 2023-02-29 does not exist: assert!; // Don't use `with_year` if you want the ordinal date to stay the same: assert_ne!;fn with_month(self: &Self, month: u32) -> Option<Self>Makes a new value with the month number (starting from 1) changed.
Errors
Returns
Nonewhen:- The resulting date does not exist (for example
month(4)when day of the month is 31). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
monthis out of range.
Examples
use ; assert_eq!; // Resulting date 2023-09-31 does not exist: assert!;Don't combine multiple
Datelike::with_*methods. The intermediate value may not exist.use ; let d = from_ymd_opt.unwrap; assert!; // fails because of invalid intermediate value // Correct version: let d = from_ymd_opt.unwrap; assert_eq!;- The resulting date does not exist (for example
fn with_month0(self: &Self, month0: u32) -> Option<Self>Makes a new value with the month number (starting from 0) changed.
Errors
Returns
Nonewhen:- The resulting date does not exist (for example
month0(3)when day of the month is 31). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
month0is out of range.
- The resulting date does not exist (for example
fn with_day(self: &Self, day: u32) -> Option<Self>Makes a new value with the day of month (starting from 1) changed.
Errors
Returns
Nonewhen:- The resulting date does not exist (for example
day(31)in April). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
dayis out of range.
- The resulting date does not exist (for example
fn with_day0(self: &Self, day0: u32) -> Option<Self>Makes a new value with the day of month (starting from 0) changed.
Errors
Returns
Nonewhen:- The resulting date does not exist (for example
day0(30)in April). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
day0is out of range.
- The resulting date does not exist (for example
fn with_ordinal(self: &Self, ordinal: u32) -> Option<Self>Makes a new value with the day of year (starting from 1) changed.
Errors
Returns
Nonewhen:- The resulting date does not exist (
with_ordinal(366)in a non-leap year). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
ordinalis out of range.
- The resulting date does not exist (
fn with_ordinal0(self: &Self, ordinal0: u32) -> Option<Self>Makes a new value with the day of year (starting from 0) changed.
Errors
Returns
Nonewhen:- The resulting date does not exist (
with_ordinal0(365)in a non-leap year). - In case of
DateTime<Tz>if the resulting date and time fall within a timezone transition such as from DST to standard time. - The value for
ordinal0is out of range.
- The resulting date does not exist (
Provided Methods
fn year_ce(self: &Self) -> (bool, u32)Returns the absolute year number starting from 1 with a boolean flag, which is false when the year predates the epoch (BCE/BC) and true otherwise (CE/AD).
fn num_days_from_ce(self: &Self) -> i32Counts the days in the proleptic Gregorian calendar, with January 1, Year 1 (CE) as day 1.
Examples
use ; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
Implementors
impl Datelike for NaiveDateimpl<Tz: TimeZone> Datelike for Date<Tz>impl<Tz: TimeZone> Datelike for DateTime<Tz>impl Datelike for NaiveDateTime