Struct Date
struct Date { ... }
Date in the proleptic Gregorian calendar.
By default, years between ±9999 inclusive are representable. This can be expanded to ±999,999
inclusive by enabling the large-dates crate feature. Doing so has performance implications
and introduces some ambiguities when parsing.
Implementations
impl Date
const fn midnight(self: Self) -> PrimitiveDateTimeCreate a
PrimitiveDateTimeusing the existing date. TheTimecomponent will be set to midnight.# use ; assert_eq!;const fn with_time(self: Self, time: Time) -> PrimitiveDateTimeCreate a
PrimitiveDateTimeusing the existing date and the providedTime.# use ; assert_eq!;const fn with_hms(self: Self, hour: u8, minute: u8, second: u8) -> Result<PrimitiveDateTime, ComponentRange>Attempt to create a
PrimitiveDateTimeusing the existing date and the provided time.# use date; assert!; assert!;const fn with_hms_milli(self: Self, hour: u8, minute: u8, second: u8, millisecond: u16) -> Result<PrimitiveDateTime, ComponentRange>Attempt to create a
PrimitiveDateTimeusing the existing date and the provided time.# use date; assert!; assert!;const fn with_hms_micro(self: Self, hour: u8, minute: u8, second: u8, microsecond: u32) -> Result<PrimitiveDateTime, ComponentRange>Attempt to create a
PrimitiveDateTimeusing the existing date and the provided time.# use date; assert!; assert!;const fn with_hms_nano(self: Self, hour: u8, minute: u8, second: u8, nanosecond: u32) -> Result<PrimitiveDateTime, ComponentRange>Attempt to create a
PrimitiveDateTimeusing the existing date and the provided time.# use date; assert!; assert!;
impl Date
const fn from_calendar_date(year: i32, month: Month, day: u8) -> Result<Self, ComponentRange>Attempt to create a
Datefrom the year, month, and day.# use ; assert!; assert!;# use ; assert!; // 2019 isn't a leap year.const fn from_ordinal_date(year: i32, ordinal: u16) -> Result<Self, ComponentRange>Attempt to create a
Datefrom the year and ordinal day number.# use Date; assert!; assert!;# use Date; assert!; // 2019 isn't a leap year.const fn from_iso_week_date(year: i32, week: u8, weekday: Weekday) -> Result<Self, ComponentRange>Attempt to create a
Datefrom the ISO year, week, and weekday.# use ; assert!; assert!; assert!;# use ; assert!; // 2019 doesn't have 53 weeks.const fn from_julian_day(julian_day: i32) -> Result<Self, ComponentRange>Create a
Datefrom the Julian day.# use Date; # use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn year(self: Self) -> i32Get the year of the date.
# use date; assert_eq!; assert_eq!; assert_eq!;const fn month(self: Self) -> MonthGet the month.
# use Month; # use date; assert_eq!; assert_eq!;const fn day(self: Self) -> u8Get the day of the month.
The returned value will always be in the range
1..=31.# use date; assert_eq!; assert_eq!;const fn ordinal(self: Self) -> u16Get the day of the year.
The returned value will always be in the range
1..=366(1..=365for common years).# use date; assert_eq!; assert_eq!;const fn iso_week(self: Self) -> u8Get the ISO week number.
The returned value will always be in the range
1..=53.# use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn sunday_based_week(self: Self) -> u8Get the week number where week 1 begins on the first Sunday.
The returned value will always be in the range
0..=53.# use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn monday_based_week(self: Self) -> u8Get the week number where week 1 begins on the first Monday.
The returned value will always be in the range
0..=53.# use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn to_calendar_date(self: Self) -> (i32, Month, u8)Get the year, month, and day.
# use Month; # use date; assert_eq!;const fn to_ordinal_date(self: Self) -> (i32, u16)Get the year and ordinal day number.
# use date; assert_eq!;const fn to_iso_week_date(self: Self) -> (i32, u8, Weekday)Get the ISO 8601 year, week number, and weekday.
# use *; # use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn weekday(self: Self) -> WeekdayGet the weekday.
# use *; # use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn next_day(self: Self) -> Option<Self>Get the next calendar date.
# use Date; # use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn previous_day(self: Self) -> Option<Self>Get the previous calendar date.
# use Date; # use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn next_occurrence(self: Self, weekday: Weekday) -> SelfCalculates the first occurrence of a weekday that is strictly later than a given
Date.Panics
Panics if an overflow occurred.
Examples
# use Weekday; # use date; assert_eq!; assert_eq!;const fn prev_occurrence(self: Self, weekday: Weekday) -> SelfCalculates the first occurrence of a weekday that is strictly earlier than a given
Date.Panics
Panics if an overflow occurred.
Examples
# use Weekday; # use date; assert_eq!; assert_eq!;const fn nth_next_occurrence(self: Self, weekday: Weekday, n: u8) -> SelfCalculates the
nth occurrence of a weekday that is strictly later than a givenDate.Panics
Panics if an overflow occurred or if
n == 0.Examples
# use Weekday; # use date; assert_eq!; assert_eq!;const fn nth_prev_occurrence(self: Self, weekday: Weekday, n: u8) -> SelfCalculates the
nth occurrence of a weekday that is strictly earlier than a givenDate.Panics
Panics if an overflow occurred or if
n == 0.Examples
# use Weekday; # use date; assert_eq!; assert_eq!;const fn to_julian_day(self: Self) -> i32Get the Julian day for the date.
# use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn checked_add(self: Self, duration: Duration) -> Option<Self>Computes
self + duration, returningNoneif an overflow occurred.# use ; # use date; assert_eq!; assert_eq!; assert_eq!;Note
This function only takes whole days into account.
# use ; # use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn checked_add_std(self: Self, duration: StdDuration) -> Option<Self>Computes
self + duration, returningNoneif an overflow occurred.# use ; # use date; assert_eq!; assert_eq!;Note
This function only takes whole days into account.
# use ; # use date; assert_eq!; assert_eq!; assert_eq!;const fn checked_sub(self: Self, duration: Duration) -> Option<Self>Computes
self - duration, returningNoneif an overflow occurred.# use ; # use date; assert_eq!; assert_eq!; assert_eq!;Note
This function only takes whole days into account.
# use ; # use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn checked_sub_std(self: Self, duration: StdDuration) -> Option<Self>Computes
self - duration, returningNoneif an overflow occurred.# use ; # use date; assert_eq!; assert_eq!;Note
This function only takes whole days into account.
# use ; # use date; assert_eq!; assert_eq!; assert_eq!;const fn saturating_add(self: Self, duration: Duration) -> SelfComputes
self + duration, saturating value on overflow.# use ; # use date; assert_eq!; assert_eq!; assert_eq!;Note
This function only takes whole days into account.
# use NumericalDuration; # use date; assert_eq!; assert_eq!;const fn saturating_sub(self: Self, duration: Duration) -> SelfComputes
self - duration, saturating value on overflow.# use ; # use date; assert_eq!; assert_eq!; assert_eq!;Note
This function only takes whole days into account.
# use NumericalDuration; # use date; assert_eq!; assert_eq!;const fn replace_year(self: Self, year: i32) -> Result<Self, ComponentRange>Replace the year. The month and day will be unchanged.
# use date; assert_eq!; assert!; // -1_000_000_000 isn't a valid year assert!; // 1_000_000_000 isn't a valid yearconst fn replace_month(self: Self, month: Month) -> Result<Self, ComponentRange>Replace the month of the year.
# use date; # use Month; assert_eq!; assert!; // 30 isn't a valid day in Februaryconst fn replace_day(self: Self, day: u8) -> Result<Self, ComponentRange>Replace the day of the month.
# use date; assert_eq!; assert!; // 0 isn't a valid day assert!; // 30 isn't a valid day in Februaryconst fn replace_ordinal(self: Self, ordinal: u16) -> Result<Self, ComponentRange>Replace the day of the year.
# use date; assert_eq!; assert!; // 0 isn't a valid ordinal assert!; // 2022 isn't a leap year
impl Add for Date
fn add(self: Self, duration: Duration) -> <Self as >::OutputPanics
This may panic if an overflow occurs.
impl Add for Date
fn add(self: Self, duration: StdDuration) -> <Self as >::OutputPanics
This may panic if an overflow occurs.
impl AddAssign for Date
fn add_assign(self: &mut Self, rhs: Duration)Panics
This may panic if an overflow occurs.
impl AddAssign for Date
fn add_assign(self: &mut Self, rhs: StdDuration)Panics
This may panic if an overflow occurs.
impl Clone for Date
fn clone(self: &Self) -> Date
impl Copy for Date
impl Debug for Date
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result<(), Error>
impl Display for Date
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Eq for Date
impl Freeze for Date
impl Hash for Date
fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)
impl Ord for Date
fn cmp(self: &Self, other: &Date) -> Ordering
impl PartialEq for Date
fn eq(self: &Self, other: &Date) -> bool
impl PartialOrd for Date
fn partial_cmp(self: &Self, other: &Date) -> Option<Ordering>
impl RefUnwindSafe for Date
impl Send for Date
impl SmartDisplay for Date
fn metadata(self: &Self, _: FormatterOptions) -> Metadata<'_, Self>fn fmt_with_metadata(self: &Self, f: &mut Formatter<'_>, metadata: Metadata<'_, Self>) -> Result
impl StructuralPartialEq for Date
impl Sub for Date
fn sub(self: Self, duration: Duration) -> <Self as >::OutputPanics
This may panic if an overflow occurs.
impl Sub for Date
fn sub(self: Self, duration: StdDuration) -> <Self as >::OutputPanics
This may panic if an overflow occurs.
impl Sub for Date
fn sub(self: Self, other: Self) -> <Self as >::Output
impl SubAssign for Date
fn sub_assign(self: &mut Self, rhs: Duration)Panics
This may panic if an overflow occurs.
impl SubAssign for Date
fn sub_assign(self: &mut Self, rhs: StdDuration)Panics
This may panic if an overflow occurs.
impl Sync for Date
impl Unpin for Date
impl UnsafeUnpin for Date
impl UnwindSafe for Date
impl<T> Any for Date
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Date
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Date
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Date
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Date
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Date
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> ToString for Date
fn to_string(self: &Self) -> String
impl<T, U> Into for Date
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Date
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Date
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>