Skip to content

parseDuration

Parses a duration string into milliseconds

844 bytes
since v12.6.0

Usage

Parse a human-readable duration string (like “1 hour”, “2 seconds”) into milliseconds.

import * as
import _
_
from 'radashi'
import _
_
.
function parseDuration(duration: _.DurationString): number (+1 overload)

Parse a duration string into a number.

By default, the following units are supported:

  • week
  • day
  • hour
  • minute
  • second
  • millisecond

By default, months and years are not supported, since these aren't likely to be useful in the majority of cases and they introduce ambiguity due to leap years and length differences between months.

parseDuration
('1 second') // => 1_000
import _
_
.
function parseDuration(duration: _.DurationString): number (+1 overload)

Parse a duration string into a number.

By default, the following units are supported:

  • week
  • day
  • hour
  • minute
  • second
  • millisecond

By default, months and years are not supported, since these aren't likely to be useful in the majority of cases and they introduce ambiguity due to leap years and length differences between months.

parseDuration
('1h') // => 3_600_000
import _
_
.
function parseDuration(duration: _.DurationString): number (+1 overload)

Parse a duration string into a number.

By default, the following units are supported:

  • week
  • day
  • hour
  • minute
  • second
  • millisecond

By default, months and years are not supported, since these aren't likely to be useful in the majority of cases and they introduce ambiguity due to leap years and length differences between months.

parseDuration
('1 hour') // => 3_600_000
import _
_
.
function parseDuration(duration: _.DurationString): number (+1 overload)

Parse a duration string into a number.

By default, the following units are supported:

  • week
  • day
  • hour
  • minute
  • second
  • millisecond

By default, months and years are not supported, since these aren't likely to be useful in the majority of cases and they introduce ambiguity due to leap years and length differences between months.

parseDuration
('1.5 hours') // => 5_400_000
import _
_
.
function parseDuration(duration: _.DurationString): number (+1 overload)

Parse a duration string into a number.

By default, the following units are supported:

  • week
  • day
  • hour
  • minute
  • second
  • millisecond

By default, months and years are not supported, since these aren't likely to be useful in the majority of cases and they introduce ambiguity due to leap years and length differences between months.

parseDuration
('-1h') // => -3_600_000

You may use the DurationParser class instead, which is more efficient for repeated parsing.

import {
class DurationParser<TUnit extends string = never, TShortUnit extends string = never>

Parses a duration string into its numeric value.

You can use parseDuration instead for a light wrapper that doesn't require the new keyword.

See

parseDuration parseDuration

for more information.

@version12.6.0

DurationParser
} from 'radashi'
const
const parser: DurationParser<never, never>
parser
= new
new DurationParser<never, never>(options?: DurationParser.Options<never, never> | undefined): DurationParser<never, never>

Parses a duration string into its numeric value.

You can use parseDuration instead for a light wrapper that doesn't require the new keyword.

See

parseDuration parseDuration

for more information.

@version12.6.0

DurationParser
()
const parser: DurationParser<never, never>
parser
.
QuantityParser<"week" | "day" | "hour" | "minute" | "second" | "millisecond", "w" | "d" | "h" | "m" | "s" | "ms">.parse(quantity: QuantityString<"week" | "day" | "hour" | "minute" | "second" | "millisecond", "w" | "d" | "h" | "m" | "s" | "ms">): number

Parse a quantity string into its numeric value

@throws{Error} If the quantity string is invalid or contains an unknown unit

parse
('1 hour') // => 3_600_000
const parser: DurationParser<never, never>
parser
.
QuantityParser<"week" | "day" | "hour" | "minute" | "second" | "millisecond", "w" | "d" | "h" | "m" | "s" | "ms">.parse(quantity: QuantityString<"week" | "day" | "hour" | "minute" | "second" | "millisecond", "w" | "d" | "h" | "m" | "s" | "ms">): number

Parse a quantity string into its numeric value

@throws{Error} If the quantity string is invalid or contains an unknown unit

parse
('1ms') // => 1
const parser: DurationParser<never, never>
parser
.
QuantityParser<"week" | "day" | "hour" | "minute" | "second" | "millisecond", "w" | "d" | "h" | "m" | "s" | "ms">.parse(quantity: QuantityString<"week" | "day" | "hour" | "minute" | "second" | "millisecond", "w" | "d" | "h" | "m" | "s" | "ms">): number

Parse a quantity string into its numeric value

@throws{Error} If the quantity string is invalid or contains an unknown unit

parse
('1.5 hours') // => 5_400_000

The units supported by default are:

  • millisecond (alias: ms)
  • second (alias: s)
  • minute (alias: m)
  • hour (alias: h)
  • day (alias: d)
  • week (alias: w)

Years and months are not supported by default, because both vary in length (e.g. leap years, not all months have 30 days). See the next section for how to add custom units.

Custom units

You may pass additional units to the parseDuration function.

import * as
import _
_
from 'radashi'
const
const customUnits: {
readonly units: {
readonly month: number;
};
readonly short: {
readonly mo: "month";
};
}
customUnits
= {
units: {
readonly month: number;
}
units
: {
month: number
month
: 30 * 24 * 60 * 60 * 1000,
},
short: {
readonly mo: "month";
}
short
: {
mo: "month"
mo
: 'month',
},
} as
type const = {
readonly units: {
readonly month: number;
};
readonly short: {
readonly mo: "month";
};
}
const
import _
_
.
function parseDuration<"month", "mo">(duration: _.DurationString<"month", "mo">, options: _.DurationParser<TUnit extends string = never, TShortUnit extends string = never>.Options<"month", "mo">): number (+1 overload)

Parse a duration string into a number.

By default, the following units are supported:

  • week
  • day
  • hour
  • minute
  • second
  • millisecond

By default, months and years are not supported, since these aren't likely to be useful in the majority of cases and they introduce ambiguity due to leap years and length differences between months.

parseDuration
('1 month',
const customUnits: {
readonly units: {
readonly month: number;
};
readonly short: {
readonly mo: "month";
};
}
customUnits
)
// => 2_592_000_000