dreamworkAPI
Ctrl K
GitHub
libraryDocs / time

time

librarysharedmenusource

A library for working with time and date.

All timestamps are Unix timestamps (time since 1970-01-01T00:00:00Z in the given unit) and are always in UTC; the local timezone is only applied when a timestamp is turned into a calendar date or a string, or parsed back from one (time.parse, time.format, time.toISOString, ...).

By default the local timezone follows the host, including its daylight saving rules. Assigning a different value to time.zone switches to a fixed UTC offset without DST.

Classes & types

Fields

NameTypeDescription
zonenumberThe standard (non-DST) UTC offset of the local timezone in hours, may be fractional (e.g. 5.5). Assign a different value to use a fixed UTC offset instead of the host's timezone.
dstbooleanWhether the host's timezone is currently in daylight saving time.
zone_dstnumberThe UTC offset of the host's timezone during daylight saving time in hours, equal to zone if the host has no DST.

Functions

time.addAdds a duration to a timestamp.
time.civilFromDaysConverts a day count relative to the Unix epoch (1970-01-01), as produced by time.daysFromCivil, back into a civil (Gre…
time.civilFromTimestampConverts a Unix timestamp in seconds into a civil (Gregorian) date and time of day.
time.dayOfYearReturns the ordinal day of the year for a given civil (Gregorian) date, where January 1st is 1.
time.daysFromCivilConverts a civil (Gregorian) calendar date into a day count relative to the Unix epoch (1970-01-01), i.e. the same scal…
time.daysInMonthReturns the number of days in a given month of a given year, accounting for leap years.
time.daysInYearReturns the number of days in a given year, accounting for leap years.
time.elapsedReturns the time elapsed since lua/game was started.
time.formatConverts a timestamp to a formatted string.
time.fromDurationConverts a duration string to the specified unit.
time.fromISOStringParses a JS-like ( ISO 8601 ) date-time string, as produced by JavaScript's Date.prototype.toISOString() / JSON.stringi…
time.fromRFC2822Parses an RFC 2822 date-time string ( as used in email Date: headers, for example Mon, 15 Jan 2024 10:30:00 +0000 ) and…
time.isLeapYearChecks if a year is a leap year, following the Gregorian rule: divisible by 4, except century years (divisible by 100),…
time.isoWeekReturns the ISO 8601 week-numbering year and week number for a given civil (Gregorian) date.
time.isoWeeksInYearReturns the number of ISO 8601 weeks in a given ISO week-numbering year.
time.lastMonthWeekdayReturns the last occurrence of a given weekday in a given month of a given year, e.g. "the last Sunday of March 2026".
time.monthWeekdayReturns the index-th occurrence of a given weekday in a given month of a given year, e.g. "the second Sunday of March 2…
time.nextWeekdayReturns the next weekday after the given one, wrapping from 6 (Saturday) back to 0 (Sunday).
time.nowReturns the current Unix timestamp (UTC) in the specified unit.
time.offsetReturns the UTC offset of the local timezone at the given moment, in seconds.
time.parseReturns a table with the date and time components.
time.prevWeekdayReturns the weekday before the given one, wrapping from 0 (Sunday) back to 6 (Saturday).
time.subSubtracts a duration from a timestamp.
time.tickReturns the time elapsed since the last call to this function (or since the library was loaded).
time.timestampFromCivilConverts a civil (Gregorian) date and time of day into a Unix timestamp in seconds.
time.toDurationConverts a number of seconds to a duration string.
time.toISOStringConverts a timestamp to a JS-like ( ISO 8601 ) date-time string, for example 2024-01-15T10:30:00.000Z or 2024-01-15T10:…
time.toRFC2822Converts a timestamp to an RFC 2822 date-time string, for example Mon, 15 Jan 2024 10:30:00 +0000.
time.transformTransforms a timestamp to a different unit.
time.weekdayDifferenceReturns the number of days to add to weekday y to reach weekday x, both given as 0-based weekday indices in [0, 6] (the…
time.weekdayFromCivilReturns the day of the week for a given civil (Gregorian) date.
time.weekdayFromDaysReturns the day of the week for a given day count relative to the Unix epoch, as produced by time.daysFromCivil.

#time.add

sharedmenunodiscardsource
function time.add(timestamp?: number, unit?: time.Unit, duration_str: time.DurationString, as_float?: boolean): number

Adds a duration to a timestamp.

The duration string can have the following units: ns, us, ms, s, m, h, d, w, y.

mo and y are calendar months and years in local time: the wall-clock time is kept and the day is clamped to the target month (Jan 31 + 1mo is Feb 28/29). All other units are exact amounts of time.

SuffixNameValue
nsNanosecond1 / 1,000,000,000 seconds
usMicrosecond1 / 1,000,000 seconds
msMillisecond1 / 1,000 seconds
sSecond1 second
mMinute60 seconds
hHour60 minutes
dDay24 hours
wWeek7 days
moMonth1 calendar month
yYear1 calendar year
Parameters
NameTypeDescription
timestampoptionalnumberThe timestamp to add the duration to, current time by default.
unitoptionaltime.UnitThe unit of the timestamp, 's' by default.
duration_strtime.DurationStringThe duration string to add.
as_floatoptionalbooleanWhether to return the timestamp as a float, false by default.
Returns
TypeNameDescription
numbernew_timestamp

#time.civilFromDays

sharedmenunodiscardsource
function time.civilFromDays(days?: integer): integer, integer, integer

Converts a day count relative to the Unix epoch (1970-01-01), as produced by time.daysFromCivil, back into a civil (Gregorian) calendar year, month, and day. The inverse of time.daysFromCivil.

Based on Howard Hinnant's "civil_from_days" algorithm, the counterpart to daysFromCivil's "days_from_civil".

Parameters
NameTypeDescription
daysoptionalintegerThe number of days since the Unix epoch. If omitted, the current local date is returned.
Returns
TypeNameDescription
integeryearThe calendar year.
integermonthThe calendar month (1-12).
integerdayThe calendar day of the month.

#time.civilFromTimestamp

sharedmenunodiscardsource
function time.civilFromTimestamp(timestamp?: number): integer, integer, integer, integer, integer, number, integer

Converts a Unix timestamp in seconds into a civil (Gregorian) date and time of day. The inverse of time.timestampFromCivil.

Like time.timestampFromCivil, this is pure arithmetic and is not affected by the host's local timezone; the result is in UTC, add time.offset( timestamp ) to get local time.

Parameters
NameTypeDescription
timestampoptionalnumberThe Unix timestamp in seconds, current time by default.
Returns
TypeNameDescription
integeryearThe calendar year.
integermonthThe calendar month (1-12).
integerdayThe calendar day of the month.
integerhourThe hour (0-23).
integerminuteThe minute (0-59).
numbersecondThe second (0-59), fractional if timestamp was.
integerweekdayThe 0-based weekday index (0 = Sunday, 6 = Saturday).

#time.dayOfYear

sharedmenunodiscardsource
function time.dayOfYear(year?: integer, month?: integer, day?: integer): integer

Returns the ordinal day of the year for a given civil (Gregorian) date, where January 1st is 1.

Parameters
NameTypeDescription
yearoptionalintegerThe calendar year, the current year by default.
monthoptionalintegerThe calendar month (1-12), the current month by default.
dayoptionalintegerThe calendar day of the month, the current day by default.
Returns
TypeNameDescription
integerday_of_yearThe day of the year (1-366).

#time.daysFromCivil

sharedmenunodiscardsource
function time.daysFromCivil(year?: integer, month?: integer, day?: integer): integer

Converts a civil (Gregorian) calendar date into a day count relative to the Unix epoch (1970-01-01), i.e. the same scale as days in time.civilFromDays and time.weekdayFromDays — negative for dates before the epoch, 0 for the epoch date itself, positive after.

Based on Howard Hinnant's "days_from_civil" algorithm, which is valid for any year representable by the host's integer type, correctly accounting for the Gregorian leap year rule (including the century/400-year exceptions) without needing a lookup table.

See howardhinnant date algorithms and socantre lua port.

Parameters
NameTypeDescription
yearoptionalintegerThe calendar year. Defaults to the current local year.
monthoptionalintegerThe calendar month (1-12). Defaults to the current local month.
dayoptionalintegerThe calendar day of the month. Defaults to the current local day.
Returns
TypeNameDescription
integerdaysThe number of days since the Unix epoch (1970-01-01).

#time.daysInMonth

sharedmenunodiscardsource
function time.daysInMonth(month?: integer, year?: integer): integer

Returns the number of days in a given month of a given year, accounting for leap years.

The result is always in the range [28, 31].

month is wrapped into the valid [1, 12] range via modulo before use, so out-of-range values (e.g. 13 or 0) are normalized rather than erroring.

Parameters
NameTypeDescription
monthoptionalintegerThe month to get the number of days in (1–12), the current month by default.
yearoptionalintegerThe year to get the number of days in, the current year by default.
Returns
TypeNameDescription
integerdays_in_monthThe number of days in the month.

#time.daysInYear

sharedmenunodiscardsource
function time.daysInYear(year?: integer): integer

Returns the number of days in a given year, accounting for leap years.

Parameters
NameTypeDescription
yearoptionalintegerThe year to get the number of days in, the current year by default.
Returns
TypeNameDescription
integerdays_in_yearThe number of days in the year, 365 or 366.

#time.elapsed

sharedmenunodiscardsource
function time.elapsed(unit?: time.Unit, as_float?: boolean): number

Returns the time elapsed since lua/game was started.

Parameters
NameTypeDescription
unitoptionaltime.UnitThe unit to return the elapsed time in, 's' by default.
as_floatoptionalbooleanWhether to return the elapsed time as a float, true by default.
Returns
TypeNameDescription
numbertimestampThe elapsed time in the specified unit.

#time.format

sharedmenunodiscardsource
function time.format(fmt: string, timestamp?: integer, unit?: time.Unit, in_utc?: boolean): string

Converts a timestamp to a formatted string.

Format Keys
KeyDescriptionExample
{day}Day of the month [01–31]16
{weekday}Weekday number [0–6, Sunday = 0]3
{weekday_name}Full weekday nameWednesday
{weekday_short_name}Abbreviated weekday nameWed
{month}Month number [01–12]09
{month_name}Full month nameSeptember
{month_short_name}Abbreviated month nameSep
{year}Full year1998
{year_day}Day of the year [001–366]259
{year_week}Week number of the year, Monday first [00–53]37
{year_short}Two-digit year [00–99]98
{hours}Hour in 24-hour format [00–23]23
{minutes}Minute [00–59]48
{seconds}Second [00–59]10
{milliseconds}Millisecond [000–999]010
{microseconds}Microsecond [000–999]010
{nanoseconds}Nanosecond [000–999]010
{hours12}Hour in 12-hour format [01–12]11
{period}AM or PMPM
{date}Localized date (same as {month}/{day}/{year})09/16/98
{time}Localized time (same as {hours}:{minutes}:{seconds})23:48:10
{datetime}Localized full date and timeWed Sep 16 23:48:10 1998
{timezone}Timezone offset, +0000 in UTC-0300
Parameters
NameTypeDescription
fmtstringThe format string.
timestampoptionalintegerThe timestamp to format.
unitoptionaltime.UnitThe timestamp unit, 's' by default.
in_utcoptionalbooleanUse UTC instead of local timezone, false by default.
Returns
TypeNameDescription
stringstrThe formatted string.

#time.fromDuration

sharedmenunodiscardsource
function time.fromDuration(duration_str: time.DurationString, unit?: time.Unit, as_float?: boolean): integer

Converts a duration string to the specified unit.

The duration string can have the following units: ns, us, ms, s, m, h, d, w, y. Numbers without a unit are seconds.

SuffixNameValue
nsNanosecond1 / 1,000,000,000 seconds
usMicrosecond1 / 1,000,000 seconds
msMillisecond1 / 1,000 seconds
sSecond1 second
mMinute60 seconds
hHour60 minutes
dDay24 hours
wWeek7 days
moMonth~30 days
yYear365 days
Parameters
NameTypeDescription
duration_strtime.DurationStringThe duration string to convert.
unitoptionaltime.UnitThe unit to convert the duration to, 's' by default.
as_floatoptionalbooleanWhether to return the duration as a float, false by default.
Returns
TypeNameDescription
integertimestampThe duration in the specified unit.

#time.fromISOString

sharedmenunodiscardsource
function time.fromISOString(str: string, unit?: time.Unit, as_float?: boolean): number

Parses a JS-like ( ISO 8601 ) date-time string, as produced by JavaScript's Date.prototype.toISOString() / JSON.stringify( new Date() ), and converts it to a unix timestamp.

Supported formats ( matching JavaScript's Date Time String Format ):

  • 2024-01-15T10:30:00.000Z - UTC, with milliseconds
  • 2024-01-15T10:30:00Z - UTC, without milliseconds
  • 2024-01-15T10:30:00.000+02:00 / 2024-01-15T10:30:00+0200 - explicit offset
  • 2024-01-15T10:30Z - without seconds
  • 2024-01-15T10:30:00 - no timezone info, interpreted as local time ( DST-aware )
  • 2024-01-15 - date only, interpreted as UTC midnight, matching JavaScript's behaviour

A space can be used instead of T. Fractions of any length are supported, digits beyond the precision of unit are truncated.

Parameters
NameTypeDescription
strstringThe JS-like date-time string to parse.
unitoptionaltime.UnitThe unit to return the timestamp in, s by default.
as_floatoptionalbooleanWhether to return the timestamp as a float, false by default.
Returns
TypeNameDescription
numbertimestampThe unix timestamp in the specified unit.

#time.fromRFC2822

sharedmenunodiscardsource
function time.fromRFC2822(str: string, unit?: time.Unit, as_float?: boolean): number

Parses an RFC 2822 date-time string ( as used in email Date: headers, for example Mon, 15 Jan 2024 10:30:00 +0000 ) and converts it to a unix timestamp.

The weekday name, the seconds and the timezone are all optional. The timezone can be a numeric offset ( +0200, -0500 ), or one of the named zones UT, GMT, UTC, EST, EDT, CST, CDT, MST, MDT, PST, PDT. A missing or unrecognised timezone is treated as UTC, per RFC 2822's handling of the obsolete military zones.

Parameters
NameTypeDescription
strstringThe RFC 2822 date-time string to parse.
unitoptionaltime.UnitThe unit to return the timestamp in, s by default.
as_floatoptionalbooleanWhether to return the timestamp as a float, false by default.
Returns
TypeNameDescription
numbertimestampThe unix timestamp in the specified unit.

#time.isLeapYear

sharedmenunodiscardsource
function time.isLeapYear(year?: integer): boolean

Checks if a year is a leap year, following the Gregorian rule: divisible by 4, except century years (divisible by 100), unless also divisible by 400.

Parameters
NameTypeDescription
yearoptionalintegerThe year to check, the current local year by default.
Returns
TypeNameDescription
booleanis_leap_yearReturns true if the year is a leap year, otherwise false.

#time.isoWeek

sharedmenunodiscardsource
function time.isoWeek(year?: integer, month?: integer, day?: integer): integer, integer, integer

Returns the ISO 8601 week-numbering year and week number for a given civil (Gregorian) date.

ISO weeks start on Monday, and week 1 is the week containing the year's first Thursday (equivalently, the week containing January 4th). Because of that, the first few days of January may belong to the last week of the previous ISO year, and the last few days of December may belong to week 1 of the next one — which is why the ISO year is returned alongside the week.

See ISO week date.

Parameters
NameTypeDescription
yearoptionalintegerThe calendar year, the current year by default.
monthoptionalintegerThe calendar month (1-12), the current month by default.
dayoptionalintegerThe calendar day of the month, the current day by default.
Returns
TypeNameDescription
integeriso_yearThe ISO week-numbering year, which may differ from year by one near year boundaries.
integerweekThe ISO week number (1-53).
integeriso_weekdayThe ISO weekday (1 = Monday, 7 = Sunday).

#time.isoWeeksInYear

sharedmenunodiscardsource
function time.isoWeeksInYear(year?: integer): integer

Returns the number of ISO 8601 weeks in a given ISO week-numbering year.

A year has 53 ISO weeks if it starts on a Thursday, or if it is a leap year that starts on a Wednesday; otherwise it has 52.

Parameters
NameTypeDescription
yearoptionalintegerThe ISO week-numbering year, the current year by default.
Returns
TypeNameDescription
integerweeksThe number of ISO weeks in the year, 52 or 53.

#time.lastMonthWeekday

sharedmenunodiscardsource
function time.lastMonthWeekday(weekday: integer, month?: integer, year?: integer): integer, integer

Returns the last occurrence of a given weekday in a given month of a given year, e.g. "the last Sunday of March 2026".

Based on Howard Hinnant's year_month_weekday_last: takes the last day of the month, then steps back by the forward distance from weekday to that day's weekday.

month is wrapped into the valid [1, 12] range via modulo before use, the same way time.daysInMonth does.

See howardhinnant date.

Parameters
NameTypeDescription
weekdayintegerThe 0-based weekday index (0 = Sunday, 6 = Saturday) to look for.
monthoptionalintegerThe month to search in (1–12), the current month by default.
yearoptionalintegerThe year to search in, the current year by default.
Returns
TypeNameDescription
integerdayThe day of the month (22-31) of the last occurrence of weekday.
integerdaysThe same date as a day count relative to the Unix epoch, as produced by time.daysFromCivil.

#time.monthWeekday

sharedmenunodiscardsource
function time.monthWeekday(weekday: integer, index: integer, month?: integer, year?: integer): integer | nil, integer | nil

Returns the index-th occurrence of a given weekday in a given month of a given year, e.g. "the second Sunday of March 2026".

Based on Howard Hinnant's year_month_weekday: takes the first day of the month, steps forward by the distance from that day's weekday to weekday, then adds whole weeks.

month is wrapped into the valid [1, 12] range via modulo before use, the same way time.daysInMonth does.

Negative index values count from the end of the month instead: -1 is the last occurrence (same as time.lastMonthWeekday), -2 the second-to-last, and so on.

If the month has no such occurrence (e.g. a 5th Monday in a month that only has four), or index is 0, nil is returned.

See howardhinnant date.

Parameters
NameTypeDescription
weekdayintegerThe 0-based weekday index (0 = Sunday, 6 = Saturday) to look for.
indexintegerThe 1-based occurrence of weekday within the month (1-5), or a negative index counting from the end of the month (-1--5).
monthoptionalintegerThe month to search in (1–12), the current month by default.
yearoptionalintegerThe year to search in, the current year by default.
Returns
TypeNameDescription
integer | nildayThe day of the month (1-31) of the index-th occurrence of weekday, or nil if the month has no such occurrence.
integer | nildaysThe same date as a day count relative to the Unix epoch, as produced by time.daysFromCivil, or nil if the month has no such occurrence.

#time.nextWeekday

sharedmenunodiscardsource
function time.nextWeekday(weekday: integer): integer

Returns the next weekday after the given one, wrapping from 6 (Saturday) back to 0 (Sunday).

Parameters
NameTypeDescription
weekdayintegerThe current weekday index (0-6).
Returns
TypeNameDescription
integernext_weekdayThe following weekday index (0-6).

#time.now

sharedmenunodiscardsource
function time.now(unit?: time.Unit, as_float?: boolean): integer | number

Returns the current Unix timestamp (UTC) in the specified unit.

Parameters
NameTypeDescription
unitoptionaltime.UnitThe unit to return the current time in, s by default.
as_floatoptionalbooleanWhether to return the timestamp as a float, false by default.
Returns
TypeNameDescription
integer | numbertimestampThe current timestamp in the specified unit.

#time.offset

sharedmenunodiscardsource
function time.offset(timestamp?: number): integer

Returns the UTC offset of the local timezone at the given moment, in seconds.

Follows the host's timezone including its DST rules, or time.zone as a fixed offset if it was changed.

Parameters
NameTypeDescription
timestampoptionalnumberThe Unix timestamp in seconds, current time by default.
Returns
TypeNameDescription
integeroffsetThe UTC offset in seconds, e.g. 7200 for +02:00.

#time.parse

sharedmenunodiscardsource
function time.parse(timestamp?: number, unit?: time.Unit, in_utc?: boolean): time.DateInfo

Returns a table with the date and time components.

Parameters
NameTypeDescription
timestampoptionalnumberThe timestamp to parse, current time by default.
unitoptionaltime.UnitThe unit to parse the timestamp from, 's' by default.
in_utcoptionalbooleanWhether to return the date in UTC instead of local time, false by default.
Returns
TypeNameDescription
time.DateInfodate_infoThe date and time components.

#time.prevWeekday

sharedmenunodiscardsource
function time.prevWeekday(weekday: integer): integer

Returns the weekday before the given one, wrapping from 0 (Sunday) back to 6 (Saturday).

Parameters
NameTypeDescription
weekdayintegerThe current weekday index (0-6).
Returns
TypeNameDescription
integerprevious_weekdayThe preceding weekday index (0-6).

#time.sub

sharedmenunodiscardsource
function time.sub(timestamp?: number, unit?: time.Unit, duration_str: time.DurationString, as_float?: boolean): number

Subtracts a duration from a timestamp.

The duration string can have the following units: ns, us, ms, s, m, h, d, w, y.

mo and y are calendar months and years in local time: the wall-clock time is kept and the day is clamped to the target month (Mar 31 - 1mo is Feb 28/29). All other units are exact amounts of time.

SuffixNameValue
nsNanosecond1 / 1,000,000,000 seconds
usMicrosecond1 / 1,000,000 seconds
msMillisecond1 / 1,000 seconds
sSecond1 second
mMinute60 seconds
hHour60 minutes
dDay24 hours
wWeek7 days
moMonth1 calendar month
yYear1 calendar year
Parameters
NameTypeDescription
timestampoptionalnumberThe timestamp to subtract the duration from, current time by default.
unitoptionaltime.UnitThe unit of the timestamp, 's' by default.
duration_strtime.DurationStringThe duration string to subtract.
as_floatoptionalbooleanWhether to return the timestamp as a float, false by default.
Returns
TypeNameDescription
numbernew_timestamp

#time.tick

sharedmenusource
function time.tick(unit?: time.Unit, as_float?: boolean): number

Returns the time elapsed since the last call to this function (or since the library was loaded).

Parameters
NameTypeDescription
unitoptionaltime.UnitThe unit to return the elapsed time in, 's' by default.
as_floatoptionalbooleanWhether to return the elapsed time as a float, true by default.
Returns
TypeNameDescription
numberdeltaThe elapsed time in the specified unit.

#time.timestampFromCivil

sharedmenunodiscardsource
function time.timestampFromCivil(year?: integer, month?: integer, day?: integer, hour?: integer, minute?: integer, second?: number): number

Converts a civil (Gregorian) date and time of day into a Unix timestamp in seconds.

Unlike os.time, this is pure arithmetic: the result is not affected by the host's local timezone or DST settings, and works for any year, including those before 1970. The date and time are treated as UTC; subtract time.offset() to convert from local time.

Parameters
NameTypeDescription
yearoptionalintegerThe calendar year, the current year by default.
monthoptionalintegerThe calendar month (1-12), the current month by default.
dayoptionalintegerThe calendar day of the month, the current day by default.
houroptionalintegerThe hour (0-23), 0 by default.
minuteoptionalintegerThe minute (0-59), 0 by default.
secondoptionalnumberThe second (0-59), 0 by default; may be fractional.
Returns
TypeNameDescription
numbertimestampThe Unix timestamp in seconds.

#time.toDuration

sharedmenunodiscardsource
function time.toDuration(timestamp: integer, unit?: time.Unit): string

Converts a number of seconds to a duration string.

The duration string can have the following units: ns, us, ms, s, m, h, d, w, y. Negative durations have every segment negated (-1m -30s), so they parse back with time.fromDuration.

SuffixNameValue
nsNanosecond1 / 1,000,000,000 seconds
usMicrosecond1 / 1,000,000 seconds
msMillisecond1 / 1,000 seconds
sSecond1 second
mMinute60 seconds
hHour60 minutes
dDay24 hours
wWeek7 days
moMonth~30 days
yYear365 days
Parameters
NameTypeDescription
timestampintegerThe timestamp to convert.
unitoptionaltime.UnitThe unit to convert the timestamp to, 's' by default.
Returns
TypeNameDescription
stringduration_strThe duration string.

#time.toISOString

sharedmenunodiscardsource
function time.toISOString(timestamp?: integer, unit?: time.Unit, in_utc?: boolean): string

Converts a timestamp to a JS-like ( ISO 8601 ) date-time string, for example 2024-01-15T10:30:00.000Z or 2024-01-15T10:30:00.000-05:00 with timezone offset.

Milliseconds are included if unit is ms, us or ns, or if timestamp has a fraction.

The output is compatible with JavaScript's Date.prototype.toISOString() / JSON.stringify( new Date() ), and can be parsed back with time.fromISOString.

Parameters
NameTypeDescription
timestampoptionalintegerThe timestamp to convert, current time by default.
unitoptionaltime.UnitThe unit of timestamp, s by default.
in_utcoptionalbooleanWhether to format the string in UTC ( with a Z suffix ), false by default.
Returns
TypeNameDescription
stringstrThe JS-like date-time string.

#time.toRFC2822

sharedmenunodiscardsource
function time.toRFC2822(timestamp?: integer, unit?: time.Unit, in_utc?: boolean): string

Converts a timestamp to an RFC 2822 date-time string, for example Mon, 15 Jan 2024 10:30:00 +0000.

This is the format used by email headers ( Date: ) and HTTP's obsolete rfc1123-date, and is also what JavaScript's Date.prototype.toString() timezone-less variants are based on.

Parameters
NameTypeDescription
timestampoptionalintegerThe timestamp to convert, current time by default.
unitoptionaltime.UnitThe unit of timestamp, s by default.
in_utcoptionalbooleanWhether to format the string in UTC, false by default.
Returns
TypeNameDescription
stringstrThe RFC 2822 date-time string.

#time.transform

sharedmenunodiscardsource
function time.transform(timestamp: number, unit?: time.Unit, target?: time.Unit, as_float?: boolean): number

Transforms a timestamp to a different unit.

A double holds ~16 significant digits, so a Unix timestamp in ns (~1.7e18) is only precise to ~256ns, and the same moment in float seconds to ~240ns.

Parameters
NameTypeDescription
timestampnumberThe timestamp to transform.
unitoptionaltime.UnitThe unit to transform the timestamp from, 's' by default.
targetoptionaltime.UnitThe unit to transform the timestamp to, 's' by default.
as_floatoptionalbooleanWhether to return the timestamp as a float, false by default.
Returns
TypeNameDescription
numbertimestampThe transformed timestamp.

#time.weekdayDifference

sharedmenunodiscardsource
function time.weekdayDifference(x: integer, y: integer): integer

Returns the number of days to add to weekday y to reach weekday x, both given as 0-based weekday indices in [0, 6] (the same convention as time.weekdayFromDays).

The result is always in [0, 6]: 0 if x and y are the same weekday, otherwise the forward (wrapping) distance from y to x.

Parameters
NameTypeDescription
xintegerThe target weekday index (0-6).
yintegerThe starting weekday index (0-6).
Returns
TypeNameDescription
integerdifferenceThe number of days forward from y to reach x, in [0, 6].

#time.weekdayFromCivil

sharedmenunodiscardsource
function time.weekdayFromCivil(year?: integer, month?: integer, day?: integer): integer

Returns the day of the week for a given civil (Gregorian) date.

Shortcut for time.weekdayFromDays( time.daysFromCivil( year, month, day ) ).

Parameters
NameTypeDescription
yearoptionalintegerThe calendar year, the current year by default.
monthoptionalintegerThe calendar month (1-12), the current month by default.
dayoptionalintegerThe calendar day of the month, the current day by default.
Returns
TypeNameDescription
integerweekdayThe 0-based weekday index (0 = Sunday, 6 = Saturday) for that date.

#time.weekdayFromDays

sharedmenunodiscardsource
function time.weekdayFromDays(days: integer): integer

Returns the day of the week for a given day count relative to the Unix epoch, as produced by time.daysFromCivil.

The result is a 0-based weekday index in [0, 6], matching the convention used by time.nextWeekday/time.prevWeekday/time.weekdayDifference; 1970-01-01 (days == 0) was a Thursday, which is why the calculation offsets by 4 before taking the modulo.

Parameters
NameTypeDescription
daysintegerThe number of days since the Unix epoch.
Returns
TypeNameDescription
integerweekdayThe 0-based weekday index (0 = Sunday, 6 = Saturday) for that day.
Generated from the dreamwork meta branch.