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

Combined date and time. Date is in the proleptic Gregorian calendar and clock time is with nanosecond precision.

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

Implementations

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

let date_time = DateTime::now();
assert!(2021 < date_time.get(DateTimeUnit::Year));

Creates a new DateTime instance from year, month, day (day of month), hour, minute and seconds.

Returns an OutOfRange error if the provided values are invalid.

let date_time = DateTime::from_ymdhms(2022, 05, 02, 12, 32, 1).unwrap();
assert_eq!("2022/05/02 12:32:01", date_time.format("yyyy/MM/dd HH:mm:ss"));

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

Returns an OutOfRange error if the provided values are invalid.

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

Creates a new DateTime instance from hour, minute and seconds.

Returns an OutOfRange error if the provided values are invalid.

let date_time = DateTime::from_hms(12, 32, 12).unwrap();
assert_eq!("0001/01/01 12:32:12", date_time.format("yyyy/MM/dd HH:mm:ss"));

Creates a new DateTime 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_time = DateTime::from_timestamp(0).unwrap();
assert_eq!("1970/01/01 00:00:00", date_time.format("yyyy/MM/dd HH:mm:ss"));

Creates a new DateTime instance from days.

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

Creates a new DateTime instance from seconds.

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

let date_time = DateTime::from_seconds(86400).unwrap();
assert_eq!("0001/01/02", date_time.format("yyyy/MM/dd"));

Creates a new DateTime instance from nanoseconds.

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

let date_time = DateTime::from_nanoseconds(86_400_000_000_000).unwrap();
assert_eq!("0001/01/02", date_time.format("yyyy/MM/dd"));

Creates a new DateTime instance from an RFC3339 timestamp string.

let date_time = DateTime::parse_rfc3339("2022-05-02T15:30:20Z").unwrap();
assert_eq!("2022/05/02 15:30:20", date_time.format("yyyy/MM/dd HH:mm:ss"));

Creates a new DateTime with the specified amount of nanoseconds set as clock time.

Returns an OutOfRange error if the provided nanoseconds are invalid (over 86_399_999_999_999)

let date_time = DateTime::from_days(738276).set_time(3_600_000_000_000).unwrap();
assert_eq!("2022/05/02 01:00:00", date_time.format("yyyy/MM/dd HH:mm:ss"));

Returns the date.

let date_time = DateTime::from_days(123);
let date = date_time.date();
assert_eq!(123, date.as_days());

Returns the clock time.

let date_time = DateTime::from_days(0).set_time(3_600_000_000_000).unwrap();
let time = date_time.time();
assert_eq!(3_600_000_000_000, time.as_nanoseconds());

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

let date_time = DateTime::from_ymd(1, 1, 2).unwrap();
assert_eq!(1, date_time.as_days());

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

let date_time = DateTime::from_ymd(1, 1, 2).unwrap();
assert_eq!(86400, date_time.as_seconds());

Returns the number of nanoseconds since January 1, 0001 00:00:00 UTC. (Negative if date is before)

let date_time = DateTime::from_ymd(1, 1, 2).unwrap();
assert_eq!(86_400_000_000_000, date_time.as_nanoseconds());

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

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

Returns the number of seconds between two DateTime instances.

let date_time1 = DateTime::from_ymd(1970, 1, 1).unwrap();
let date_time2 = DateTime::from_ymd(1970, 1, 2).unwrap();
assert_eq!(86400, date_time1.between(&date_time2));
assert_eq!(86400, date_time2.between(&date_time1));

Get a specific DateTimeUnit.

let date_time = DateTime::from_ymdhms(2022, 5, 2, 12, 32, 1).unwrap();
assert_eq!(2022, date_time.get(DateTimeUnit::Year));
assert_eq!(5, date_time.get(DateTimeUnit::Month));
assert_eq!(32, date_time.get(DateTimeUnit::Min));

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

Note: When using DateTimeUnit::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_time = DateTime::from_ymdhms(1970, 1, 1, 12, 32, 1).unwrap();
let applied = date_time.apply(1, DateTimeUnit::Day).unwrap();
assert_eq!("1970-01-01 12:32:01", date_time.format("yyyy-MM-dd HH:mm:ss"));
assert_eq!("1970-01-02 12:32:01", applied.format("yyyy-MM-dd HH:mm:ss"));
let applied_2 = applied.apply(-1, DateTimeUnit::Hour).unwrap();
assert_eq!("1970-01-02 11:32:01", applied_2.format("yyyy-MM-dd HH:mm:ss"));

Creates a new DateTime instance with a specific DateTimeUnit set to the provided value.

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

let date_time = DateTime::from_ymdhms(2022, 5, 2, 12, 32, 1).unwrap();
assert_eq!(2000, date_time.set(2000, DateTimeUnit::Year).unwrap().get(DateTimeUnit::Year));
assert_eq!(10, date_time.set(10, DateTimeUnit::Min).unwrap().get(DateTimeUnit::Min));

Format as an RFC3339 timestamp (2022-05-02T15:30:20Z).

Use the Precision enum to specify decimal places after seconds:

let date_time = DateTime::from_ymdhms(2022, 5, 2, 15, 30, 20).unwrap();
assert_eq!("2022-05-02T15:30:20Z", date_time.format_rfc3339(Precision::Seconds));
// Equivalent to:
assert_eq!("2022-05-02T15:30:20Z", date_time.format("yyyy-MM-ddTHH:mm:ssXXX"));

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
AM, PMa..aaAM, PM
aaaam, pm*
aaaaa.m., p.m.
aaaaaa, p
AM, PM,
noon, midnight
b..bbAM, PM,
noon, midnight
bbbam, pm,
noon, midnight
*
bbbba.m., p.m.,
noon, midnight
bbbbba, p, n, mi
hourh1, 12[1-12]
hh01, 12*
H0, 23[0-23]
HH00, 23*
K0, 11[0-11]
KK00, 11*
k1, 24[1-24]
kk01, 24*
minutem0, 59
mm00, 59*
seconds0, 59
ss00, 59*
subsecond valuesn1, 9Deciseconds
nn01, 99Centiseconds
nnn001, 999Milliseconds, *
nnnn000001, 999999Microseconds
nnnnn000000001, 999999999Nanoseconds
zoneX-08, +0530, Z
XX-0800, Z
XXX-08:00, Z*
XXXX-0800, -075258, Z
XXXXX-08:00, -07:52:58, Z
x-08, +0530, +00Like X but without Z
xx-0800, +0000
xxx-08:00, +00:00*
xxxx-0800, -075258, +0000
xxxxx-08:00, -07:52:58, +00:00

* = 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_time = DateTime::from_ymdhms(2022, 5, 2, 12, 32, 1).unwrap();
assert_eq!("2022/05/02 12:32:01", date_time.format("yyyy/MM/dd HH:mm:ss"));
// Escape characters
assert_eq!("2022/MM/dd 12:32:01", date_time.format("yyyy/'MM/dd' HH:mm:ss"));
assert_eq!("2022/'05/02' 12:32:01", date_time.format("yyyy/''MM/dd'' HH:mm:ss"));

Creates a new DateTime instance with a given timezone offset defined as time units (hour, minute and second). Offset can range anywhere from UTC-23:59:59 to UTC+23:59:59.

The offset affects all format functions and the get and set functions but does not change the datetime itself which always represents UTC.

Returns an OutOfRange error if the provided offset is either not between UTC-23:59:59 and UTC+23:59:59 or if it would lead to an out of range date.

let date_time = DateTime::from_ymdhms(2022, 5, 2, 12, 32, 1).unwrap();
// Set offset to UTC+2
let with_offset = date_time.set_offset(7200).unwrap();
assert_eq!("2022/05/02 14:32:01", with_offset.format("yyyy/MM/dd HH:mm:ss"));

Creates a new DateTime instance with a given timezone offset defined as seconds. Offset can range anywhere from UTC-23:59:59 to UTC+23:59:59.

The offset affects all format functions and the get and set functions but does not change the datetime itself which always represents UTC.

Returns an OutOfRange error if the provided offset is either not between UTC-23:59:59 and UTC+23:59:59 or if it would lead to an out of range date.

let date_time = DateTime::from_ymdhms(2022, 5, 2, 12, 32, 1).unwrap();
// Set offset to UTC+2
let with_offset = date_time.set_offset(7200).unwrap();
assert_eq!("2022/05/02 14:32:01", with_offset.format("yyyy/MM/dd HH:mm:ss"));

Creates a new DateTime instance, assuming the current instance has the provided offset applied. The new instance will have the specified offset and the datetime itself will be converted to UTC.

The offset affects all format functions and the get and set functions but does not change the datetime itself which always represents UTC.

Returns an OutOfRange error if the provided offset is either not between UTC-23:59:59 and UTC+23:59:59 or if it would lead to an out of range date.

let date_time = DateTime::from_ymdhms(2022, 5, 2, 12, 32, 1).unwrap();
// Set offset to UTC+2
let with_offset = date_time.as_offset_time(2, 0, 0, Offset::East).unwrap();
assert_eq!("2022/05/02 12:32:01", with_offset.format("yyyy/MM/dd HH:mm:ss"));

Creates a new DateTime instance, assuming the current instance has the provided offset applied. The new instance will have the specified offset and the datetime itself will be converted to UTC.

The offset affects all format functions and the get and set functions but does not change the datetime itself which always represents UTC.

Returns an OutOfRange error if the provided offset is either not between UTC-23:59:59 and UTC+23:59:59 or if it would lead to an out of range date.

let date_time = DateTime::from_ymdhms(2022, 5, 2, 12, 32, 1).unwrap();
// Set offset to UTC+2
let with_offset = date_time.as_offset(7200).unwrap();
assert_eq!("2022/05/02 12:32:01", with_offset.format("yyyy/MM/dd HH:mm:ss"));

Returns the set offset in seconds.

let date_time = DateTime::now().set_offset(3600).unwrap();
assert_eq!(3600, date_time.get_offset());

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.