@elemental/common / Class

LmnDuration

@class LmnDuration @description A wrapper class for managing durations, offering support for various formats, timezones, and date manipulation.

This class provides utilities to work with durations, allowing:

  • Conversion between different duration formats (e.g., milliseconds, ISO, human-readable).
  • Duration manipulation (e.g., adding/subtracting hours, minutes, seconds).

Constructor

Creates a new LmnDuration instance.

Presentation
constructor(
	value: string | number | Partial<Record<"milliseconds" | "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years", number>>
): LmnDuration;
Parameters
NameTypeDescription
value
string | number | Partial<Record<"milliseconds" | "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years", number>>

@param value The time from which you want to create a LmnDuration, expressed as milliseconds or as an ISO string or as an object representing the different duration parameters.

Properties

NameTypeDescription
#duration
WritableSignal<Duration>
extendedTime
r
Signal<string>

The current duration stored in the LmnDuration, formatted as an extended hms string (i.e. "5hr 4min 25sec"). This format is the most suitable for screen readers. It is updated whenever the duration changes.

fullTime
r
Signal<string>

The current duration stored in the LmnDuration, formatted as a full hms string (i.e. "5 hours 4 minutes 25 seconds"). It is updated whenever the duration changes.

time
r
Signal<string>

The current duration stored in the LmnDuration, formatted as a hms string (i.e. "5h 4m 25s"). It is updated whenever the duration changes.

Methods

add()

Adds the provided amount of time to the current LmnDuration.

Presentation
add(value: number | Partial<Record<"milliseconds" | "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years", number>>): LmnDuration;
Parameters
NameTypeDescription
value
number | Partial<Record<"milliseconds" | "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years", number>>

@param value The amount of time to add, expressed as milliseconds or as an object representing the different duration parameters.

Returns
LmnDuration -

The LmnDuration object.

asDays()

Returns the length of the duration in days.

Presentation
asDays(): number;
Returns
number -

The length of the duration in days.

asHours()

Returns the length of the duration in hours.

Presentation
asHours(): number;
Returns
number -

The length of the duration in hours.

asMilliseconds()

Returns the length of the duration in milliseconds.

Presentation
asMilliseconds(): number;
Returns
number -

The length of the duration in milliseconds.

asMinutes()

Returns the length of the duration in minutes.

Presentation
asMinutes(): number;
Returns
number -

The length of the duration in minutes.

asMonths()

Returns the length of the duration in months.

Presentation
asMonths(): number;
Returns
number -

The length of the duration in months.

asSeconds()

Returns the length of the duration in seconds.

Presentation
asSeconds(): number;
Returns
number -

The length of the duration in seconds.

asWeeks()

Returns the length of the duration in weeks.

Presentation
asWeeks(): number;
Returns
number -

The length of the duration in weeks.

asYears()

Returns the length of the duration in years.

Presentation
asYears(): number;
Returns
number -

The length of the duration in years.

clone()

Returns a copy of this LmnDuration object

Presentation
clone(): LmnDuration;
Returns

format()

Formats the current LmnDuration following the provided options or the defaults.

If no options are provided, the LmnDuration is formatted as an hms duration (i.e. "4h 3m 29s").

It is possible to format the LmnDuration as an hms time or an hms extended time:

  • myDuration.format({ as: 'time' }) // i.e. "4h 3m 29s"
  • myDuration.format({ as: 'extended-time' }) // i.e. "4hr 3min 29sec"

By default, durations are formatted dropping the leading and trailing blank units. However, it is possible to keep them when formatting a duration:

  • myDuration.format({ as: 'time', dropBlankUnits: true }) // i.e. "3m 29s"
  • myDuration.format({ as: 'time', dropBlankUnits: false }) // i.e. "0h 3m 29s"

It is also possible to provide a totally custom format:

  • myDuration.format({ format: 'Y [years and] M [months]' }) // i.e. "4 years and 5 months"
  • myDuration.format({ format: 'SSS[ms]' }) // i.e. "375ms"
Presentation
format(options: LmnDurationFormatOptions = {}): string;
Parameters
NameTypeDescription
options
LmnDurationFormatOptions

@param options Format options

Returns
string -

The formatted duration

getDays()

Returns the days.

Presentation
getDays(): number;
Returns
number -

The days.

getHours()

Returns the hours (0-23).

Presentation
getHours(): number;
Returns
number -

The hours.

getMilliseconds()

Returns the milliseconds (0-999).

Presentation
getMilliseconds(): number;
Returns
number -

The milliseconds.

getMinutes()

Returns the minutes (0-59).

Presentation
getMinutes(): number;
Returns
number -

The minutes.

getMonths()

Returns the months.

Presentation
getMonths(): number;
Returns
number -

The months.

getSeconds()

Returns the seconds (0-59).

Presentation
getSeconds(): number;
Returns
number -

The seconds.

getWeeks()

Returns the weeks.

Presentation
getWeeks(): number;
Returns
number -

The weeks.

getYears()

Returns the years.

Presentation
getYears(): number;
Returns
number -

The years.

humanize()

Returns the humanized duration, approximated to its biggest unit (i.e. "4 days", "3 minutes").

Presentation
humanize(): string;
Returns
string -

The humanized duration.

subtract()

Subtracts the provided amount of time from the current LmnDuration.

Presentation
subtract(value: number | Partial<Record<"milliseconds" | "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years", number>>): LmnDuration;
Parameters
NameTypeDescription
value
number | Partial<Record<"milliseconds" | "seconds" | "minutes" | "hours" | "days" | "weeks" | "months" | "years", number>>

@param value The amount of time to subtract, expressed as milliseconds or as an object representing the different duration parameters.

Returns
LmnDuration -

The LmnDuration object.

toISOString()

Formats the current LmnDuration to the ISO8601 standard.

Presentation
toISOString(): string;
Returns
string -

The ISO8601 formatted duration.