Struct Date
pub struct Date { /* private fields */ }
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) -> PlainDateTimeCreate a
PlainDateTimeusing the existing date. TheTimecomponent will be set to midnight.# use ; assert_eq!;const fn with_time(self, time: Time) -> PlainDateTimeCreate a
PlainDateTimeusing the existing date and the providedTime.# use ; assert_eq!;const fn with_hms(self, hour: u8, minute: u8, second: u8) -> Result<PlainDateTime, ComponentRange>Attempt to create a
PlainDateTimeusing the existing date and the provided time.# use date; assert!; assert!;const fn with_hms_milli(self, hour: u8, minute: u8, second: u8, millisecond: u16) -> Result<PlainDateTime, ComponentRange>Attempt to create a
PlainDateTimeusing the existing date and the provided time.# use date; assert!; assert!;const fn with_hms_micro(self, hour: u8, minute: u8, second: u8, microsecond: u32) -> Result<PlainDateTime, ComponentRange>Attempt to create a
PlainDateTimeusing the existing date and the provided time.# use date; assert!; assert!;const fn with_hms_nano(self, hour: u8, minute: u8, second: u8, nanosecond: u32) -> Result<PlainDateTime, ComponentRange>Attempt to create a
PlainDateTimeusing the existing date and the provided time.# use date; assert!; assert!;
impl Date
const MIN: Self = _;The minimum valid
Date.The value of this may vary depending on the feature flags enabled.
const MAX: Self = _;The maximum valid
Date.The value of this may vary depending on the feature flags enabled.
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) -> i32Get the year of the date.
# use date; assert_eq!; assert_eq!; assert_eq!;const fn month(self) -> MonthGet the month.
# use Month; # use date; assert_eq!; assert_eq!;const fn day(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) -> 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) -> 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) -> 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) -> 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) -> (i32, Month, u8)Get the year, month, and day.
# use Month; # use date; assert_eq!;const fn to_ordinal_date(self) -> (i32, u16)Get the year and ordinal day number.
# use date; assert_eq!;const fn to_iso_week_date(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) -> 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) -> Option<Self>Get the next calendar date.
# use Date; # use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn previous_day(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, 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, 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, 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, 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 iter_to(self, end: Self) -> DateIterCreate an iterator of dates from
selftoendinclusive.# use date; let mut iter = date!.iter_to; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn to_julian_day(self) -> i32Get the Julian day for the date.
# use date; assert_eq!; assert_eq!; assert_eq!; assert_eq!;const fn checked_add(self, duration: SignedDuration) -> 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, 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, duration: SignedDuration) -> 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, 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, duration: SignedDuration) -> 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, duration: SignedDuration) -> 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, 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, 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, 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, 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
Trait Implementations
impl Add<Duration> for Date
type Output = Date;fn add(self, duration: StdDuration) -> Self::OutputPanics
This may panic if an overflow occurs.
impl Add<SignedDuration> for Date
type Output = Date;fn add(self, duration: SignedDuration) -> Self::OutputPanics
This may panic if an overflow occurs.
impl AddAssign<Duration> for Date
fn add_assign(&mut self, rhs: StdDuration)Panics
This may panic if an overflow occurs.
impl AddAssign<SignedDuration> for Date
fn add_assign(&mut self, rhs: SignedDuration)Panics
This may panic if an overflow occurs.
impl Clone for Date
fn clone(&self) -> Self
impl Copy for Date
impl Debug for Date
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl Display for Date
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for Date
impl Hash for Date
fn hash<__H: Hasher>(&self, state: &mut __H)
impl Ord for Date
fn cmp(&self, other: &Self) -> Ordering
impl PartialEq for Date
fn eq(&self, other: &Self) -> bool
impl PartialOrd for Date
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
impl SmartDisplay for Date
type Metadata = ();fn metadata(&self, FormatterOptions) -> Metadata<'_, Self>fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl StructuralPartialEq for Date
impl Sub for Date
type Output = SignedDuration;fn sub(self, other: Self) -> Self::Output
impl Sub<Duration> for Date
type Output = Date;fn sub(self, duration: StdDuration) -> Self::OutputPanics
This may panic if an overflow occurs.
impl Sub<SignedDuration> for Date
type Output = Date;fn sub(self, duration: SignedDuration) -> Self::OutputPanics
This may panic if an overflow occurs.
impl SubAssign<Duration> for Date
fn sub_assign(&mut self, rhs: StdDuration)Panics
This may panic if an overflow occurs.
impl SubAssign<SignedDuration> for Date
fn sub_assign(&mut self, rhs: SignedDuration)Panics
This may panic if an overflow occurs.
Auto Trait Implementations
impl Freeze for Date
impl RefUnwindSafe for Date
impl Send for Date
impl Sync for Date
impl Unpin for Date
impl UnsafeUnpin for Date
impl UnwindSafe for Date
Blanket Implementations
impl<T> Any for Date
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Date
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Date
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Date
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Date
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Date
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T> ToString for Date
where
T: Display + ?Sized,
fn to_string(&self) -> String
impl<T, U> Into<U> for Date
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Date
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Date
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>