Skip to content
SinisterRectus edited this page Oct 23, 2022 · 9 revisions

Represents a single moment in time and provides utilities for converting to and from different date and time formats. Although microsecond precision is available, most formats are implemented with only second precision.

Constructor

Date(seconds, microseconds)

Parameter Type Optional
seconds number
microseconds number

Static Methods

fromHeader(str)

Parameter Type
str string

Constructs a new Date object from an RFC 2822 string. Equivalent to Date(Date.parseHeader(str)).

This method only operates on data in memory.

Returns: Date


fromISO(str)

Parameter Type
str string

Constructs a new Date object from an ISO 8601 string. Equivalent to Date(Date.parseISO(str)).

This method only operates on data in memory.

Returns: Date


fromMicroseconds(us)

Parameter Type
us number

Constructs a new Date object from a Unix time in microseconds.

This method only operates on data in memory.

Returns: Date


fromMilliseconds(ms)

Parameter Type
ms number

Constructs a new Date object from a Unix time in milliseconds.

This method only operates on data in memory.

Returns: Date


fromSeconds(s)

Parameter Type
s number

Constructs a new Date object from a Unix time in seconds.

This method only operates on data in memory.

Returns: Date


fromSnowflake(id)

Parameter Type
id string

Constructs a new Date object from a Discord/Twitter Snowflake ID. Equivalent to Date(Date.parseSnowflake(id)).

This method only operates on data in memory.

Returns: Date


fromTable(tbl)

Parameter Type
tbl table

Constructs a new Date object from a Lua date table interpreted as a local time. Equivalent to Date(Date.parseTable(tbl)).

This method only operates on data in memory.

Returns: Date


fromTableUTC(tbl)

Parameter Type
tbl table

Constructs a new Date object from a Lua date table interpreted as a UTC time. Equivalent to Date(Date.parseTableUTC(tbl)).

This method only operates on data in memory.

Returns: Date


parseHeader(str)

Parameter Type
str string

Converts an RFC 2822 string (an HTTP Date header) into a Unix time in seconds.

This method only operates on data in memory.

Returns: number


parseISO(str)

Parameter Type
str string

Converts an ISO 8601 string into a Unix time in seconds. For compatibility with Discord's timestamp format, microseconds are also provided as a second return value.

This method only operates on data in memory.

Returns: number, number


parseSnowflake(id)

Parameter Type
id string

Converts a Discord Snowflake ID into a Unix time in seconds. Additional decimal points may be present, though only the first 3 (milliseconds) should be considered accurate.

This method only operates on data in memory.

Returns: number


parseTable(tbl)

Parameter Type Optional
tbl table

Interprets a Lua date table as a local time and converts it to a Unix time in seconds. Equivalent to os.time(tbl).

This method only operates on data in memory.

Returns: number


parseTableUTC(tbl)

Parameter Type Optional
tbl table

Interprets a Lua date table as a UTC time and converts it to a Unix time in seconds. Equivalent to os.time(tbl) with a correction for UTC.

This method only operates on data in memory.

Returns: number


Methods

toHeader()

Returns an RFC 2822 string that represents the stored date and time.

This method only operates on data in memory.

Returns: string


toISO(sep, tz)

Parameter Type Optional
sep string
tz string

Returns an ISO 8601 string that represents the stored date and time. If sep and tz are both provided, then they are used as a custom separator and timezone; otherwise, T is used for the separator and +00:00 is used for the timezone, plus microseconds if available.

This method only operates on data in memory.

Returns: string


toMicroseconds()

Returns a Unix time in microseconds that represents the stored date and time.

This method only operates on data in memory.

Returns: number


toMilliseconds()

Returns a Unix time in milliseconds that represents the stored date and time.

This method only operates on data in memory.

Returns: number


toParts()

Returns the seconds and microseconds that are stored in the date object.

This method only operates on data in memory.

Returns: number, number


toSeconds()

Returns a Unix time in seconds that represents the stored date and time.

This method only operates on data in memory.

Returns: number


toSnowflake()

Returns a synthetic Discord Snowflake ID based on the stored date and time. Due to the lack of native 64-bit support, the result may lack precision. In other words, Date.fromSnowflake(id):toSnowflake() == id may be false.

This method only operates on data in memory.

Returns: string


toString(fmt)

Parameter Type Optional
fmt string

Returns a string from this Date object via Lua's os.date. If no format string is provided, the default is '%a %b %d %Y %T GMT%z (%Z)'.

This method only operates on data in memory.

Returns: string


toTable()

Returns a Lua date table that represents the stored date and time as a local time. Equivalent to os.date('*t', s) where s is the Unix time in seconds.

This method only operates on data in memory.

Returns: table


toTableUTC()

Returns a Lua date table that represents the stored date and time as a UTC time. Equivalent to os.date('!*t', s) where s is the Unix time in seconds.

This method only operates on data in memory.

Returns: table


Clone this wiki locally