Was this page helpful?
Duration¶
types~Duration(months, days, nanoseconds)
Represents a duration. A duration stores separately months, days, and seconds due to the fact that the number of days in a month varies, and a day can have 23 or 25 hours if a daylight saving is involved.
Constructor¶
new Duration(months, days, nanoseconds)
Creates a new instance of Duration.
Parameters:
| Name | Type | Description |
|---|---|---|
months |
The number of months. |
|
days |
The number of days. |
|
nanoseconds |
The number of nanoseconds. |
Members¶
(readonly) days
Gets the number of days
(readonly) months
Gets the number of months
(readonly) nanoseconds
Gets the number of nanoseconds
Methods¶
toBuffer()
Serializes the duration and returns the representation of the value in bytes.
toString()
Returns the string representation of the value.
(static) fromBuffer()
Creates a new Duration instance from the binary representation of the value.
(static) fromString()
Creates a new Duration instance from the string representation of the value.
Accepted formats:
- multiple digits followed by a time unit like: 12h30m where the time unit can be:
y: yearsmo: monthsw: weeksd: daysh: hoursm: minutess: secondsms: millisecondsusorµs: microsecondsns: nanoseconds
- ISO 8601 format:
P[n]Y[n]M[n]DT[n]H[n]M[n]S or P[n]W - ISO 8601 alternative format:
P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]
Duration can be made negative by adding - at the beginning of the input
Examples
From formatted string
let date = fromString("4mo7d20ns"); // 1 month, 7 days, 20 nanosecondsFrom ISO 8601
let date = fromString("P2DT5M"); // 2 days, 5 minutes