DateTime
struct DateTime
DateTime is a struct that represents a date and time.
to_timezone
pub fn to_timezone(self, tz: TimeZone): DateTime
Converts the DateTime to a different TimeZone.
to_timezone
pub fn to_timezone(self, tz: TimeZone): DateTime
Converts the DateTime to a different TimeZone.
Converts the DateTime to a different TimeZone.
Example
use std.time.{DateTime, TimeZone};
let t = try! DateTime.parse("2023-04-13 09:45:26.123456789 +08:00");
let tz = TimeZone.load("America/New_York")!;
let t = t.to_timezone(tz);
assert_eq t.to_string(), "2023-04-12T21:45:26.123456789-04:00";
Return the time elapsed from the given time to now.
Return the time elapsed from the given time to now.
Return the time elapsed from the given time to now.
to_offset
pub fn to_offset(self, offset: int): DateTime throws InvalidDateTimeError
Return this DateTime
into offset.
to_offset
pub fn to_offset(self, offset: int): DateTime throws InvalidDateTimeError
Return this DateTime
into offset.
Return this DateTime
into offset.
The offset
is the number of whole seconds the offset is from UTC
.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26.123456789 +08:00");
let utc_t = try! t.to_offset(0);
assert_eq utc_t.hour(), 1;
assert_eq utc_t.minute(), 45;
assert_eq utc_t.to_string(), "2023-04-13T01:45:26.123456789Z";
// Pacific Time
let t1 = try! t.to_offset(-28800);
assert_eq t1.hour(), 17;
assert_eq t1.minute(), 45;
assert_eq t1.to_string(), "2023-04-12T17:45:26.123456789-08:00";
// New Delhi
let t2 = try! t.to_offset(19800);
assert_eq t2.hour(), 7;
assert_eq t2.minute(), 15;
assert_eq t2.to_string(), "2023-04-13T07:15:26.123456789+05:30";
Return this DateTime
in UTC (GMT) offset.
Return this DateTime
in UTC (GMT) offset.
Return this DateTime
in UTC (GMT) offset.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26.123456789 +08:00");
let utc_t = t.utc();
assert_eq utc_t.hour(), 1;
assert_eq utc_t.to_string(), "2023-04-13T01:45:26.123456789Z";
operator>=:2
pub fn operator>=:2(self, b: DateTime): bool
operator>=:2
pub fn operator>=:2(self, b: DateTime): bool
operator>:2
pub fn operator>:2(self, b: DateTime): bool
operator>:2
pub fn operator>:2(self, b: DateTime): bool
operator<=:2
pub fn operator<=:2(self, b: DateTime): bool
operator<=:2
pub fn operator<=:2(self, b: DateTime): bool
operator<:2
pub fn operator<:2(self, b: DateTime): bool
operator<:2
pub fn operator<:2(self, b: DateTime): bool
operator!=:2
pub fn operator!=:2(self, b: DateTime): bool
operator!=:2
pub fn operator!=:2(self, b: DateTime): bool
operator==:2
pub fn operator==:2(self, b: DateTime): bool
operator==:2
pub fn operator==:2(self, b: DateTime): bool
operator+:2
pub fn operator+:2(self, b: Duration): DateTime
Return time a + b (seconds) to a new DateTime
.
operator+:2
pub fn operator+:2(self, b: Duration): DateTime
Return time a + b (seconds) to a new DateTime
.
Return time a + b (seconds) to a new DateTime
.
use std.time.DateTime;
let a = try! DateTime.parse("2023-04-13 09:45:26.215");
let b = a + 873.seconds();
// "2023-04-13 09:59:00.215"
operator-:2
pub fn operator-:2(self, b: Duration): DateTime
Return time a - b (seconds) to a new DateTime
operator-:2
pub fn operator-:2(self, b: Duration): DateTime
Return time a - b (seconds) to a new DateTime
Return time a - b (seconds) to a new DateTime
use std.time.DateTime;
let a = try! DateTime.parse("2023-04-13 09:59:00.123");
let b = a - 873.seconds();
// "2023-04-13 09:45:26.123"
Return a Duration
of the a, b time difference.
Return a Duration
of the a, b time difference.
Return a Duration
of the a, b time difference.
use std.time.DateTime;
let a = try! DateTime.parse("2023-04-13 09:45:26.215");
let b = try! DateTime.parse("2023-04-13 09:59:00.123");
let seconds = b.sub(a);
// 873.908
Return int
weekday of this date.
Return int
weekday of this date.
Return int
weekday of this date.
The returned value will always be in the range 1..7
, Monday (1) .. Sunday (7).
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26.123456789 +08:00");
assert_eq t.weekday(), 4;
Returns the number of whole seconds the offset is from UTC
.
Returns the number of whole seconds the offset is from UTC
.
Returns the number of whole seconds the offset is from UTC
.
A positive value indicates an offset to the east; a negative to the west.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26.123456789 +08:00");
assert_eq t.offset(), 28800;
set_nanosecond
pub fn set_nanosecond(self, nanosecond: int) throws InvalidDateTimeError
Set the nanosecond of this date.
set_nanosecond
pub fn set_nanosecond(self, nanosecond: int) throws InvalidDateTimeError
Set the nanosecond of this date.
Set the nanosecond of this date.
nanosecond
pub fn nanosecond(self): int
Return int
nanosecond of this date.
nanosecond
pub fn nanosecond(self): int
Return int
nanosecond of this date.
Return int
nanosecond of this date.
The returned value will always be in the range `0..1000000000``.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26.123456789 +08:00");
assert_eq t.nanosecond(), 123456789;
set_microsecond
pub fn set_microsecond(self, microsecond: int) throws InvalidDateTimeError
Set the microsecond of this date.
set_microsecond
pub fn set_microsecond(self, microsecond: int) throws InvalidDateTimeError
Set the microsecond of this date.
Set the microsecond of this date.
microsecond
pub fn microsecond(self): int
Return int
microsecond of this date.
microsecond
pub fn microsecond(self): int
Return int
microsecond of this date.
Return int
microsecond of this date.
The returned value will always be in the range 0..1000000
.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26.123456 +08:00");
assert_eq t.microsecond(), 123456;
set_millisecond
pub fn set_millisecond(self, millisecond: int) throws InvalidDateTimeError
Set the millisecond of this date.
set_millisecond
pub fn set_millisecond(self, millisecond: int) throws InvalidDateTimeError
Set the millisecond of this date.
Set the millisecond of this date.
millisecond
pub fn millisecond(self): int
Return int
millisecond of this date.
millisecond
pub fn millisecond(self): int
Return int
millisecond of this date.
Return int
millisecond of this date.
The returned value will always be in the range 0..1000
.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26.123 +08:00");
assert_eq t.millisecond(), 123;
set_second
pub fn set_second(self, second: int) throws InvalidDateTimeError
Set the second of this date.
set_second
pub fn set_second(self, second: int) throws InvalidDateTimeError
Set the second of this date.
Set the second of this date.
Return int
second of this date.
Return int
second of this date.
Return int
second of this date.
The returned value will always be in the range 0..60
.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26.123 +08:00");
assert_eq t.second(), 26;
set_minute
pub fn set_minute(self, minute: int) throws InvalidDateTimeError
Set the minute of this date.
set_minute
pub fn set_minute(self, minute: int) throws InvalidDateTimeError
Set the minute of this date.
Set the minute of this date.
Return int
minute of this date.
Return int
minute of this date.
Return int
minute of this date.
The returned value will always be in the range 0..60
.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26 +08:00");
assert_eq t.minute(), 45;
set_hour
pub fn set_hour(self, hour: int) throws InvalidDateTimeError
Set the hour of this date.
set_hour
pub fn set_hour(self, hour: int) throws InvalidDateTimeError
Set the hour of this date.
Set the hour of this date.
Return int
hour of this date.
Return int
hour of this date.
Return int
hour of this date.
The returned value will always be in the range 0..24
.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26 +08:00");
assert_eq t.hour(), 9;
let t = try! DateTime.parse("2023-04-13 19:45:26 +08:00");
assert_eq t.hour(), 19;
set_day
pub fn set_day(self, day: int) throws InvalidDateTimeError
Set the day of this date.
set_day
pub fn set_day(self, day: int) throws InvalidDateTimeError
Set the day of this date.
Set the day of this date.
Return int
day of this date.
Return int
day of this date.
Return int
day of this date.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26 +08:00");
assert_eq t.day(), 13;
set_month
pub fn set_month(self, month: int) throws InvalidDateTimeError
Set the month of this date.
set_month
pub fn set_month(self, month: int) throws InvalidDateTimeError
Set the month of this date.
Set the month of this date.
Return int
month of this date.
Return int
month of this date.
Return int
month of this date.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26 +08:00");
assert_eq t.month(), 4;
set_year
pub fn set_year(self, year: int) throws InvalidDateTimeError
Set the year of this date.
set_year
pub fn set_year(self, year: int) throws InvalidDateTimeError
Set the year of this date.
Set the year of this date.
Return int
year of this date.
Return int
year of this date.
Return int
year of this date.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26 +08:00");
assert_eq t.year(), 2023;
Return the time string with the ISO 8601 format in local timezone.
Return the time string with the ISO 8601 format in local timezone.
Return the time string with the ISO 8601 format in local timezone.
If the local timezone is not available, use UTC.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26 +08:00");
assert_eq t.to_string(), "2023-04-13T09:45:26+08:00";
Return the time string
with the RFC3339 format in original timezone.
Return the time string
with the RFC3339 format in original timezone.
Return the time string
with the RFC3339 format in original timezone.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26 +08:00");
assert_eq t.to_string(), "2023-04-13T09:45:26+08:00";
Return the time string
with the Time Format format.
Return the time string
with the Time Format format.
Return the time string
with the Time Format format.
If the format is invalid, return nil
.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26 +08:00");
assert_eq t.format("%Y-%m-%d %H:%M:%S %z"), "2023-04-13 09:45:26 +0800";
assert_eq t.format("%m/%d/%Y %H:%M"), "04/13/2023 09:45";
assert_eq t.format("invalid"), nil;
Return the int
Unix Timestamp (in second) of the time.
Return the int
Unix Timestamp (in second) of the time.
Return the int
Unix Timestamp (in second) of the time.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13 09:45:26 +08:00");
assert_eq t.timestamp(), 1681350326;
from_timestamp
pub fn from_timestamp(ts: int): DateTime
Return a time parse from unix timestamp (in seconds)
from_timestamp
pub fn from_timestamp(ts: int): DateTime
Return a time parse from unix timestamp (in seconds)
Return a time parse from unix timestamp (in seconds)
use std.time.DateTime;
let t = DateTime.from_timestamp(1618315526);
assert_eq t.to_string(), "2021-04-13T12:05:26Z";
Return the current time in UTC.
Return the current time in UTC.
Return the current time in UTC.
use std.time.DateTime;
let t = DateTime.now_utc();
Return the current time in the local offset.
Return the current time in the local offset.
Return the current time in the local offset.
use std.time.DateTime;
let t = DateTime.now_local();
parse
pub fn parse(time_str: string, format: string? = nil): DateTime throws ParseTimeError
Parse the time string with the Time Format format.
parse
pub fn parse(time_str: string, format: string? = nil): DateTime throws ParseTimeError
Parse the time string with the Time Format format.
Parse the time string with the Time Format format.
If the time string is not a valid time string, throw error.
Kw Args
format
- Given a time format string, parse the time string with the format (default:nil
), if is nil, will try to parse with the default format.
use std.time.DateTime;
let t = try! DateTime.parse("2023-04-13T14:08:33-11:00");
assert_eq t.to_string(), "2023-04-13T14:08:33-11:00";
let t = try! DateTime.parse("2023.04.13 Time: 14:08", format: "%Y.%m.%d Time: %H:%M");
assert_eq t.to_string(), "2023-04-13T14:08:00Z";