Returns the conversion system to use
Get the days.
Get the hours.
Returns an explanation of why this Duration became invalid, or null if the Duration is valid
Returns an error code if this Duration became invalid, or null if the Duration is valid
Returns whether the Duration is invalid. Invalid durations are returned by diff operations on invalid DateTimes or Intervals.
Get the locale of a Duration, such 'en-GB'
Get the conversion matrix of a Duration
Get the milliseconds.
Get the minutes.
Get the months.
Get the numbering system of a Duration, such 'beng'. The numbering system is used when formatting the Duration
Get the quarters.
Get the seconds.
Get the weeks
Get the years.
Return the length of the duration in the specified unit.
Duration.fromObject({years: 1}).as('days') //=> 365
Duration.fromObject({years: 1}).as('months') //=> 12
Duration.fromObject({hours: 60}).as('days') //=> 2.5
a unit such as 'minutes' or 'days'
Get the value of unit.
Duration.fromObject({years: 2, days: 3}).get('years') //=> 2
Duration.fromObject({years: 2, days: 3}).get('months') //=> 0
Duration.fromObject({years: 2, days: 3}).get('days') //=> 3
a unit such as 'minute' or 'day'
Returns the max unit in the duration, forcing the shifting to the max possible. Forcing solves having bigger units at 0, when creating with a smaller unit. Es. Duration.fromMillis(4945676146971854) By default it uses all the units, but a flag can be passed to use only Human duration units (all except quarters and weeks)
var dur = Duration.fromObject({ minutes: 61 })
dur.getMaxUnit() //=> 'hours'
Choose if using ORDERED_UNITS (default) or HUMAN_ORDERED_UNITS
Scale this Duration by the specified amount. Return a newly-constructed Duration.
Duration.fromObject({ hours: 1, minutes: 30 }).mapUnits(x => x * 2) //=> { hours: 2, minutes: 60 }
Duration.fromObject({ hours: 1, minutes: 30 }).mapUnits((x, u) => u === "hours" ? x * 2 : x) //=> { hours: 2, minutes: 30 }
The function to apply to each unit. Arity is 1 or 2: the value of the unit and, optionally, the unit name. Must return a number.
Reduce this Duration to its canonical representation in its current units.
Duration.fromObject({ years: 2, days: 5000 }).normalize().toObject() //=> { years: 15, days: 255 }
Duration.fromObject({ hours: 12, minutes: -45 }).normalize().toObject() //=> { hours: 11, minutes: 15 }
"Set" the locale and/or numberingSystem and/or conversionAccuracy. Returns a newly-constructed Duration.
dur.reconfigure({ locale: 'en-GB' })
"Set" the values of specified units. Non-specified units stay unchanged. Return a newly-constructed Duration.
dur.set({ years: 2017 })
dur.set({ hours: 8, minutes: 30 })
a mapping of units to numbers
Convert this Duration into its representation in a different set of units.
Duration.fromObject({ hours: 1, seconds: 30 }).shiftTo('minutes', 'milliseconds').toObject() //=> { minutes: 60, milliseconds: 30000 }
Rest
...units: (keyof DurationObject)[]Returns a string representation of this Duration formatted according to the specified format string. You may use these tokens:
S
for millisecondss
for secondsm
for minutesh
for hoursd
for daysw
for weeksM
for monthsy
for years
Notes:Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("y d s") //=> "1 6 2"
Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("yy dd sss") //=> "01 06 002"
Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("M S") //=> "12 518402000"
the format string
options
Returns a string representation of a Duration with all units included.
To modify its behavior use the listStyle
and any Intl.NumberFormat option, though unitDisplay
is especially relevant.
You can also exclude quarters and weeks, by passing { onlyHumanUnits: true }
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
var dur = Duration.fromObject({ days: 1, hours: 5, minutes: 6 })
dur.toHuman() //=> '1 day, 5 hours, 6 minutes'
dur.toHuman({ listStyle: "long" }) //=> '1 day, 5 hours, and 6 minutes'
dur.toHuman({ unitDisplay: "short" }) //=> '1 day, 5 hr, 6 min'
On option object to override the formatting. Accepts the same keys as the options parameter of the native Int.NumberFormat
constructor, as well as listStyle
.
Returns an ISO 8601-compliant string representation of this Duration.
https://en.wikipedia.org/wiki/ISO_8601#Durations
Duration.fromObject({ years: 3, seconds: 45 }).toISO() //=> 'P3YT45S'
Duration.fromObject({ months: 4, seconds: 45 }).toISO() //=> 'P4MT45S'
Duration.fromObject({ months: 5 }).toISO() //=> 'P5M'
Duration.fromObject({ minutes: 5 }).toISO() //=> 'PT5M'
Duration.fromObject({ milliseconds: 6 }).toISO() //=> 'PT0.006S'
Returns an ISO 8601-compliant string representation of this Duration, formatted as a time of day. Note that this will return null if the duration is invalid, negative, or equal to or greater than 24 hours.
https://en.wikipedia.org/wiki/ISO_8601#Times
Duration.fromObject({ hours: 11 }).toISOTime() //=> '11:00:00.000'
Duration.fromObject({ hours: 11 }).toISOTime({ suppressMilliseconds: true }) //=> '11:00:00'
Duration.fromObject({ hours: 11 }).toISOTime({ suppressSeconds: true }) //=> '11:00'
Duration.fromObject({ hours: 11 }).toISOTime({ includePrefix: true }) //=> 'T11:00:00.000'
Duration.fromObject({ hours: 11 }).toISOTime({ format: 'basic' }) //=> '110000.000'
options
Returns a JavaScript object with this Duration's values.
Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toObject() //=> { years: 1, days: 6, seconds: 2 }
Returns a milliseconds value of this Duration. Alias of toMillis
Static
fromStatic
fromISOCreate a Duration from an ISO 8601 duration string.
https://en.wikipedia.org/wiki/ISO_8601#Durations
Duration.fromISO('P3Y6M1W4DT12H30M5S').toObject() //=> { years: 3, months: 6, weeks: 1, days: 4, hours: 12, minutes: 30, seconds: 5 }
Duration.fromISO('PT23H').toObject() //=> { hours: 23 }
Duration.fromISO('P5Y3M').toObject() //=> { years: 5, months: 3 }
text to parse
Optional
opts: DurationOptionsoptions for parsing
Static
fromISOTimeCreate a Duration from an ISO 8601 time string.
https://en.wikipedia.org/wiki/ISO_8601#Times
Duration.fromISOTime('11:22:33.444').toObject() //=> { hours: 11, minutes: 22, seconds: 33, milliseconds: 444 }
Duration.fromISOTime('11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
Duration.fromISOTime('T11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
Duration.fromISOTime('1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
Duration.fromISOTime('T1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
text to parse
options for parsing
Static
fromCreate Duration from a number of milliseconds.
of milliseconds
options for parsing
Static
fromCreate a Duration from a JavaScript object with keys like 'years' and 'hours'. If this object is empty then a zero milliseconds duration is returned.
the object to create the DateTime from
Optional
opts: DurationOptions = {}options for creating this Duration
Static
invalidCreate an invalid Duration.
simple string of why this datetime is invalid. Should not contain parameters or anything else data-dependent
Optional
explanation: stringlonger explanation, may include parameters and other useful debugging information
Static
isGenerated using TypeDoc
A Duration object represents a period of time, like "2 months" or "1 day, 1 hour". Conceptually, it's just a map of units to their quantities, accompanied by some additional configuration and methods for creating, parsing, interrogating, transforming, and formatting them. They can be used on their own or in conjunction with other Luxon types; for example, you can use plus to add a Duration object to a DateTime, producing another DateTime. * Here is a brief overview of commonly used methods and getters in Duration:
There are more methods documented below. In addition, for more information on subtler topics like internationalization and validity, see the external documentation.