logo
pub struct Date { /* private fields */ }
Expand description

Date in the proleptic Gregorian calendar.

Range: 30. June -5879611..=12. July 5879611. Please note that year 0 does not exist. After year -1 follows year 1.

Implementations

Creates a new Date instance with SystemTime::now().

let date = Date::now();
assert!(2021 < date.get(DateUnit::Year));

Creates a new Date instance from year, month and day (day of month).

Returns an OutOfRange error if the provided values are invalid.

let date = Date::from_ymd(2022, 05, 02).unwrap();
assert_eq!("2022/05/02", date.format("yyyy/MM/dd"));

Creates a new Date instance from a unix timestamp (non-leap seconds since January 1, 1970 00:00:00 UTC).

Returns an OutOfRange error if the provided timestamp would result in an out of range date.

let date = Date::from_timestamp(0).unwrap();
assert_eq!("1970/01/01", date.format("yyyy/MM/dd"));

Creates a new Date instance from days since January 1, 0001.

let date = Date::from_days(738276);
assert_eq!("2022/05/02", date.format("yyyy/MM/dd"));

Returns the number of days since January 1, 0001. (Negative if date is before)

let date = Date::from_ymd(1, 1, 1).unwrap();
assert_eq!(0, date.as_days());

Returns the number of non-leap seconds since January 1, 1970 00:00:00 UTC. (Negative if date is before)

let date = Date::from_ymd(2000, 1, 1).unwrap();
assert_eq!(946_684_800, date.timestamp());

Returns the number of days between two Date instances.

let date = Date::from_ymd(1970, 1, 1).unwrap();
let date_2 = Date::from_ymd(1970, 2, 1).unwrap();
assert_eq!(31, date.between(&date_2));
assert_eq!(31, date_2.between(&date));

Get a specific DateUnit.

let date = Date::from_ymd(2022, 5, 2).unwrap();
assert_eq!(2022, date.get(DateUnit::Year));
assert_eq!(5, date.get(DateUnit::Month));
assert_eq!(2, date.get(DateUnit::Day));

Creates a new Date instance with a specific DateUnit set to the provided value.

Returns an OutOfRange error if the provided value is invalid or out of range.

let mut date = Date::from_ymd(2022, 5, 2).unwrap();
date = date.set(2000, DateUnit::Year).unwrap();
date = date.set(10, DateUnit::Day).unwrap();
assert_eq!("2000/05/10", date.format("yyyy/MM/dd"));

Creates a new Date instance with a specified amount of time applied (added or subtracted).

Note: When using DateUnit::Month, it adds calendar months and not 30 days. See it’s documentation for examples.

Returns an OutOfRange error if the provided value would result in an out of range date.

let date = Date::from_ymd(1970, 1, 1).unwrap();

let applied = date.apply(1, DateUnit::Day).unwrap();
assert_eq!("1970/01/01", date.format("yyyy/MM/dd"));
assert_eq!("1970/01/02", applied.format("yyyy/MM/dd"));

let applied_2 = applied.apply(-1, DateUnit::Day).unwrap();
assert_eq!("1970/01/01", applied_2.format("yyyy/MM/dd"));

Formatting with format strings based on Unicode Date Field Symbols.

Please note that not all symbols are implemented. If you need something that is not implemented, please open an issue on GitHub describing your need.

Available Symbols:
Field TypePatternExamplesHint
eraG..GGGAD
GGGGAnno Domini*
GGGGGA
yeary2, 20, 201, 2017, 20173
yy02, 20, 01, 17, 73
yyy002, 020, 201, 2017, 20173
yyyy0002, 0020, 0201, 2017, 20173
yyyyy+Unlimited length,
padded with zeros.
quarterq2*
qq02
qqqQ2
qqqq2nd quarter
qqqqq2
monthM9, 12
MM09, 12
MMMSep
MMMMSeptember*
MMMMMS
weekw8, 27Week of year
ww08, 27*
daysd1Day of month
dd01*
D1, 24 135Day of year, *
DD01, 24, 135
DDD001, 024, 135
week daye31-7, 1 is Sunday, *
ee031-7, 1 is Sunday
eeeTue
eeeeTuesday
eeeeeT
eeeeeeTu
eeeeeee21-7, 1 is Monday
eeeeeeee021-7, 1 is Monday

* = Default

If the sequence is longer than listed in the table, the output will be the same as the default pattern for this unit (marked with *).

Surround any character with apostrophes (') to escape them. If you want escape ', write ''.

let date = Date::from_ymd(2022, 5, 2).unwrap();
assert_eq!("2022/05/02", date.format("yyyy/MM/dd"));
// Escape characters
assert_eq!("2022/MM/dd", date.format("yyyy/'MM/dd'"));
assert_eq!("2022/'05/02'", date.format("yyyy/''MM/dd''"));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.