Skip to content

Interface: Duration<IsValid>

A Duration object represents a period of time, like "2 months" or "1 day, 1 hour". Conceptually, it is 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 DateTime.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's are more methods documented below. In addition, for more information on subtler topics like internationalization and validity, see the external documentation.

Type Parameters

Type ParameterDefault type
IsValid extends booleanDefaultValidity

Accessors

days

Get Signature

ts
get days(): IfValid<number, number, IsValid>;

Get the days.

Returns

IfValid<number, number, IsValid>


hours

Get Signature

ts
get hours(): IfValid<number, number, IsValid>;

Get the hours.

Returns

IfValid<number, number, IsValid>


invalidExplanation

Get Signature

ts
get invalidExplanation(): IfValid<null, string | null, IsValid>;

Returns an explanation of why this Duration became invalid, or null if the Duration is valid

Returns

IfValid<null, string | null, IsValid>


invalidReason

Get Signature

ts
get invalidReason(): IfValid<null, string, IsValid>;

Returns an error code if this Duration became invalid, or null if the Duration is valid

Returns

IfValid<null, string, IsValid>


isValid

Get Signature

ts
get isValid(): IfValid<true, false, IsValid>;

Returns whether the Duration is invalid. Diff operations on invalid DateTimes or Intervals return invalid Durations.

Returns

IfValid<true, false, IsValid>


locale

Get Signature

ts
get locale(): IfValid<string, null, IsValid>;

Get the locale of a Duration, such as 'en-GB'

Returns

IfValid<string, null, IsValid>


milliseconds

Get Signature

ts
get milliseconds(): IfValid<number, number, IsValid>;

Get the milliseconds.

Returns

IfValid<number, number, IsValid>


minutes

Get Signature

ts
get minutes(): IfValid<number, number, IsValid>;

Get the minutes.

Returns

IfValid<number, number, IsValid>


months

Get Signature

ts
get months(): IfValid<number, number, IsValid>;

Get the months.

Returns

IfValid<number, number, IsValid>


numberingSystem

Get Signature

ts
get numberingSystem(): IfValid<string, null, IsValid>;

Get the numbering system of a Duration, such as 'beng'. The numbering system is used when formatting the Duration

Returns

IfValid<string, null, IsValid>


quarters

Get Signature

ts
get quarters(): IfValid<number, number, IsValid>;

Get the quarters.

Returns

IfValid<number, number, IsValid>


seconds

Get Signature

ts
get seconds(): IfValid<number, number, IsValid>;

Get the seconds.

Returns

IfValid<number, number, IsValid>


weeks

Get Signature

ts
get weeks(): IfValid<number, number, IsValid>;

Get the weeks

Returns

IfValid<number, number, IsValid>


years

Get Signature

ts
get years(): IfValid<number, number, IsValid>;

Get the years.

Returns

IfValid<number, number, IsValid>

Methods

as()

ts
as(unit: keyof DurationLikeObject): IfValid<number, number, IsValid>;

Return the length of the duration in the specified unit.

Parameters

ParameterTypeDescription
unitkeyof DurationLikeObjecta unit such as 'minutes' or 'days'

Returns

IfValid<number, number, IsValid>

Examples

ts
Duration.fromObject({ years: 1 }).as("days"); //=> 365
ts
Duration.fromObject({ years: 1 }).as("months"); //=> 12
ts
Duration.fromObject({ hours: 60 }).as("days"); //=> 2.5

equals()

ts
equals(other: Duration): IfValid<boolean, false, IsValid>;

Equality check Two Durations are equal iff they have the same units and the same values for each unit.

Parameters

ParameterType
otherDuration

Returns

IfValid<boolean, false, IsValid>


get()

ts
get(unit: keyof DurationLikeObject): IfValid<number, number, IsValid>;

Get the value of unit.

Parameters

ParameterTypeDescription
unitkeyof DurationLikeObjecta unit such as 'minute' or 'day'

Returns

IfValid<number, number, IsValid>

Examples

ts
Duration.fromObject({ years: 2, days: 3 }).get("years"); //=> 2
ts
Duration.fromObject({ years: 2, days: 3 }).get("months"); //=> 0
ts
Duration.fromObject({ years: 2, days: 3 }).get("days"); //=> 3

mapUnits()

ts
mapUnits(fn: (x: number, u?: keyof DurationLikeObject) => number): this;

Scale this Duration by the specified amount. Return a newly-constructed Duration.

Parameters

ParameterType
fn(x: number, u?: keyof DurationLikeObject) => number

Returns

this

Examples

ts
Duration.fromObject({ hours: 1, minutes: 30 }).mapUnit((x) => x * 2); //=> { hours: 2, minutes: 60 }
ts
Duration.fromObject({ hours: 1, minutes: 30 }).mapUnit((x, u) =>
  u === "hour" ? x * 2 : x,
); //=> { hours: 2, minutes: 30 }

minus()

ts
minus(duration: DurationLike): this;

Make this Duration shorter by the specified amount. Return a newly-constructed Duration.

Parameters

ParameterTypeDescription
durationDurationLikeThe amount to subtract. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject()

Returns

this


negate()

ts
negate(): this;

Return the negative of this Duration.

Returns

this

Example

ts
Duration.fromObject({ hours: 1, seconds: 30 }).negate().toObject(); //=> { hours: -1, seconds: -30 }

normalize()

ts
normalize(): this;

Reduce this Duration to its canonical representation in its current units.

Returns

this

Examples

ts
Duration.fromObject({ years: 2, days: 5000 }).normalize().toObject(); //=> { years: 15, days: 255 }
ts
Duration.fromObject({ hours: 12, minutes: -45 }).normalize().toObject(); //=> { hours: 11, minutes: 15 }

plus()

ts
plus(duration: DurationLike): this;

Make this Duration longer by the specified amount. Return a newly-constructed Duration.

Parameters

ParameterTypeDescription
durationDurationLikeThe amount to add. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject()

Returns

this


reconfigure()

ts
reconfigure(opts?: DurationOptions): this;

"Set" the locale and/or numberingSystem. Returns a newly-constructed Duration.

Parameters

ParameterType
opts?DurationOptions

Returns

this

Example

ts
dur.reconfigure({ locale: "en-GB" });

removeZeros()

ts
removeZeros(): this;

Removes all units with values equal to 0 from this Duration.

Returns

this

Example

ts
Duration.fromObject({ years: 2, days: 0, hours: 0, minutes: 0 })
  .removeZeros()
  .toObject(); //=> { years: 2 }

rescale()

ts
rescale(): this;

Rescale units to its largest representation.

Returns

this

Example

ts
Duration.fromObject({ milliseconds: 90000 }).rescale().toObject(); //=> { minutes: 1, seconds: 30 }

set()

ts
set(values: DurationLikeObject): this;

"Set" the values of specified units. Return a newly-constructed Duration.

Parameters

ParameterTypeDescription
valuesDurationLikeObjecta mapping of units to numbers

Returns

this

Examples

ts
dur.set({ years: 2017 });
ts
dur.set({ hours: 8, minutes: 30 });

shiftTo()

ts
shiftTo(...units: keyof DurationLikeObject[]): this;

Convert this Duration into its representation in a different set of units.

Parameters

ParameterType
...unitskeyof DurationLikeObject[]

Returns

this

Example

ts
Duration.fromObject({ hours: 1, seconds: 30 })
  .shiftTo("minutes", "milliseconds")
  .toObject(); //=> { minutes: 60, milliseconds: 30000 }

shiftToAll()

ts
shiftToAll(): this;

Shift this Duration to all available units. Same as shiftTo("years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds")

Returns

this


toFormat()

ts
toFormat(fmt: string, opts?: DurationFormatOptions): IfValid<string, "Invalid Duration", IsValid>;

Returns a string representation of this Duration formatted according to the specified format string. You may use these tokens:

  • S for milliseconds
  • s for seconds
  • m for minutes
  • h for hours
  • d for days
  • M for months
  • y for years Notes:
  • Add padding by repeating the token, e.g. "yy" pads the years to two digits, "hhhh" pads the hours out to four digits
  • The duration will be converted to the set of units in the format string using Duration.shiftTo and the Duration's conversion accuracy setting.

Parameters

ParameterType
fmtstring
opts?DurationFormatOptions

Returns

IfValid<string, "Invalid Duration", IsValid>

Examples

ts
Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("y d s"); //=> "1 6 2"
ts
Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("yy dd sss"); //=> "01 06 002"
ts
Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("M S"); //=> "12 518402000"
ts
Duration.fromObject({ days: 6, seconds: 2 }).toFormat("d s", {
  signMode: "all",
}); //=> "+6 +2"
ts
Duration.fromObject({ days: -6, seconds: -2 }).toFormat("d s", {
  signMode: "all",
}); //=> "-6 -2"
ts
Duration.fromObject({ days: -6, seconds: -2 }).toFormat("d s", {
  signMode: "negativeLargestOnly",
}); //=> "-6 2"

toHuman()

ts
toHuman(opts?: ToHumanDurationOptions): string;

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. See Intl.NumberFormat.

Parameters

ParameterType
opts?ToHumanDurationOptions

Returns

string

Example

js
var dur = Duration.fromObject({ months: 1, weeks: 0, hours: 5, minutes: 6 });
dur.toHuman(); //=> '1 month, 0 weeks, 5 hours, 6 minutes'
dur.toHuman({ listStyle: "long" }); //=> '1 month, 0 weeks, 5 hours, and 6 minutes'
dur.toHuman({ unitDisplay: "short" }); //=> '1 mth, 0 wks, 5 hr, 6 min'
dur.toHuman({ showZeros: false }); //=> '1 month, 5 hours, 6 minutes'

toISO()

ts
toISO(): IfValid<string, null, IsValid>;

Returns an ISO 8601-compliant string representation of this Duration.

Returns

IfValid<string, null, IsValid>

See

https://en.wikipedia.org/wiki/ISO_8601#Durations

Examples

ts
Duration.fromObject({ years: 3, seconds: 45 }).toISO(); //=> 'P3YT45S'
ts
Duration.fromObject({ months: 4, seconds: 45 }).toISO(); //=> 'P4MT45S'
ts
Duration.fromObject({ months: 5 }).toISO(); //=> 'P5M'
ts
Duration.fromObject({ minutes: 5 }).toISO(); //=> 'PT5M'
ts
Duration.fromObject({ milliseconds: 6 }).toISO(); //=> 'PT0.006S'

toISOTime()

ts
toISOTime(opts?: ToISOTimeDurationOptions): IfValid<string, null, IsValid>;

Returns an ISO 8601-compliant string representation of this Duration, formatted as a time of day.

Parameters

ParameterTypeDescription
opts?ToISOTimeDurationOptionsoptions

Returns

IfValid<string, null, IsValid>

See

https://en.wikipedia.org/wiki/ISO_8601#Times

Examples

ts
Duration.fromObject({ hours: 11 }).toISOTime(); //=> '11:00:00.000'
ts
Duration.fromObject({ hours: 11 }).toISOTime({ suppressMilliseconds: true }); //=> '11:00:00'
ts
Duration.fromObject({ hours: 11 }).toISOTime({ suppressSeconds: true }); //=> '11:00'
ts
Duration.fromObject({ hours: 11 }).toISOTime({ includePrefix: true }); //=> 'T11:00:00.000'
ts
Duration.fromObject({ hours: 11 }).toISOTime({ format: "basic" }); //=> '110000.000'

toJSON()

ts
toJSON(): IfValid<string, null, IsValid>;

Returns an ISO 8601 representation of this Duration appropriate for use in JSON.

Returns

IfValid<string, null, IsValid>


toMillis()

ts
toMillis(): IfValid<number, number, IsValid>;

Returns a millisecond value of this Duration.

Returns

IfValid<number, number, IsValid>


toObject()

ts
toObject(): DurationObjectUnits;

Returns a JavaScript object with this Duration's values.

Returns

DurationObjectUnits

Example

ts
Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toObject(); //=> { years: 1, days: 6, seconds: 2 }

toString()

ts
toString(): IfValid<string, null, IsValid>;

Returns an ISO 8601 representation of this Duration appropriate for use in debugging.

Returns

IfValid<string, null, IsValid>


valueOf()

ts
valueOf(): IfValid<number, number, IsValid>;

Returns a millisecond value of this Duration. Alias of toMillis

Returns

IfValid<number, number, IsValid>