TimeDurationUtils
Durations. A duration is a length of time with no
start point, kept as a number of seconds to match Roblox's os.time, task.wait and friends.
A year is counted as 365 days and a month as 30 days when converting to and from
seconds.
TimeDurationUtils.format({ hours = 1, minutes = 30 }, "hh:mm") --> 01:30
TimeDurationUtils.humanize(TimeDurationUtils.toSeconds(2, "days")) --> 2 days
TimeDurationUtils.toIsoString("PT90M") --> PT1H30M
Types
DurationTable
interface DurationTable {years: number?months: number?weeks: number?days: number?hours: number?minutes: number?seconds: number?milliseconds: number?}Units of a duration. Singular keys are accepted as well.
DurationLike
A number of seconds, a DurationTable, or an ISO 8601 duration such as "P1DT12H".
DurationBreakdown
interface DurationBreakdown {years: numbermonths: numberdays: numberhours: numberminutes: numberseconds: numbermilliseconds: number}A duration broken into whole units, largest first, as TimeDurationUtils.toTable returns it.
DurationStringOverrides
type DurationStringOverrides = TimeLocalizationUtils.DurationStringOverridesDurationTrim
type DurationTrim = "large" | "small" | "both" | "mid" | "all" | false
Which zero-valued tokens TimeDurationUtils.format drops: large the leading ones, small
the trailing ones, mid the interior ones, both leading and trailing, all every one and
false none. The last remaining token is never dropped.
DurationFormatOptions
interface DurationFormatOptions {strings: DurationStringOverrides?--
Overrides the phrase for a unit, e.g. { hours = { one = "%d hr", other = "%d hrs" } }
stopTrim: string?--
Tokens never dropped, as a template such as m; * before a token in the template does the same
largest: number?--
Show only this many of the largest non-zero tokens; implies trim = "all" unless trim is given
trunc: boolean?--
Truncate the smallest token instead of rounding it
precision: number?--
Decimal places on the smallest token; negative rounds to tens, hundreds and so on
forceLength: boolean?--
Pad the first shown token to its template width even when a larger one was dropped
minValue: number?--
Below this many of the smallest unit, print < and the minimum instead
maxValue: number?--
Above this many of the smallest unit, print > and the maximum instead
limits: {[string]: number}?--
How far a token may count before the next larger token is used: { minutes = 60 } prints one hour as 60:00, { hours = 47 } prints a day and a half as 36:00:00
}Options for TimeDurationUtils.format.
Functions
toSeconds
Normalizes any DurationLike to seconds. A bare number is seconds unless unit says
otherwise, so toSeconds(5, "minutes") is 300.
toMilliseconds
Normalizes any DurationLike to milliseconds. See TimeDurationUtils.toSeconds.
as
Returns the whole duration in one unit, fractional. as(90, "minutes")
is 1.5.
toTable
Breaks the duration into whole years, months, days, hours, minutes, seconds and milliseconds, largest first.
get
Returns one whole unit of the breakdown: get({ hours = 25 }, "hours") is
1 and get({ hours = 25 }, "days") is 1. Weeks are whole weeks within the days and
quarters whole quarters within the months.
add
Adds another duration, returning seconds. unit applies when other is a number.
subtract
TimeDurationUtils.subtract() → numberSubtracts another duration, returning seconds. unit applies when other is a number.
formatUnit
TimeDurationUtils.formatUnit() → string
Prints an amount with its unit, pluralized for the locale: formatUnit("days", 1) is
1 day and formatUnit("days", 45) is 45 days. The amount is printed as given, so
formatUnit("hours", 1.5) is 1.5 hours. Quarters have no phrase.
TimeDurationUtils.formatUnit("hours", 2, { locale = "es-es" }) --> 2 horas
format
TimeDurationUtils.format() → string
Formats the duration with a template in the style of moment-duration-format. Tokens are y
years, M months, w weeks, d days, h hours, m minutes, s seconds, C
centiseconds and S milliseconds; repeating a letter zero pads it to that width. The largest token in the
template absorbs everything above it, so h:mm on 47 hours is 47:00, and the smallest
token is rounded unless trunc is set. __ after a token prints it as a localized phrase
such as 2 days, and text in square brackets is literal.
Tokens whose value is zero are dropped from the front by default, so h:mm:ss on 45 seconds
is 45; see DurationTrim. Dropping a token also drops the text between it and its
neighbour, so put unit words in __ labels rather than brackets when trimming matters.
Without a template, one is chosen from the duration's size: 250 milliseconds, 2:03:00,
3 days, 1 week, 3 days, 2 hours or 1 year, 2 months, 3 days.
TimeDurationUtils.format(47 * 3600, "h:mm:ss") --> 47:00:00
TimeDurationUtils.format(65.432, "mm:ss:CC", { trunc = true }) --> 01:05:43
TimeDurationUtils.format({ days = 45 }, "d __") --> 45 days
TimeDurationUtils.format(123 * 60, "d __ h:mm:ss") --> 2:03:00
TimeDurationUtils.format({ days = 1, minutes = 5 }, "d __, h __, m __", { largest = 2 }) --> 1 day, 5 minutes
TimeDurationUtils.format({ hours = 2 }, "h __", { locale = "es-es" }) --> 2 horas
humanize
TimeDurationUtils.humanize() → string
Describes the duration in words through [RelativeTimeUtils]:
an hour, 2 days. With withSuffix, a positive duration reads in an hour and a negative
one an hour ago. options can set the locale, thresholds or strings; its withoutSuffix
is ignored in favour of withSuffix.
toIsoString
Formats the duration as an ISO 8601 string such as P1DT12H or PT1.5S. A zero duration is P0D and a negative one is prefixed with -.