The Duration class provides time duration values with nanosecond precision and support for human-readable formats like "5h30m".
Import:
Source: value/duration.ts
Constructor
new Duration(value)
Create a new duration value.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | Duration | string | [bigint, bigint] | Value to create duration from. |
Examples
Supported Units
| Unit | Symbol | Example |
|---|---|---|
| Nanoseconds | ns | "500ns" |
| Microseconds | us, µs | "250us" |
| Milliseconds | ms | "100ms" |
| Seconds | s | "30s" |
| Minutes | m | "5m" |
| Hours | h | "2h" |
| Days | d | "7d" |
| Weeks | w | "4w" |
| Years | y | "1y" |
Static Methods
Duration.nanoseconds(ns)
Create a duration from a number of nanoseconds.
Parameters
| Parameter | Type | Description |
|---|---|---|
ns | number | bigint | Number of nanoseconds. |
Returns
Duration - Duration representing the given nanoseconds
Example
Duration.microseconds(µs)
Create a duration from a number of microseconds.
Parameters
| Parameter | Type | Description |
|---|---|---|
µs | number | bigint | Number of microseconds. |
Returns
Duration - Duration representing the given microseconds
Example
Duration.milliseconds(ms)
Create a duration from a number of milliseconds.
Parameters
| Parameter | Type | Description |
|---|---|---|
ms | number | bigint | Number of milliseconds. |
Returns
Duration - Duration representing the given milliseconds
Example
Duration.seconds(s)
Create a duration from a number of seconds.
Parameters
| Parameter | Type | Description |
|---|---|---|
s | number | bigint | Number of seconds. |
Returns
Duration - Duration representing the given seconds
Example
Duration.minutes(m)
Create a duration from a number of minutes.
Parameters
| Parameter | Type | Description |
|---|---|---|
m | number | bigint | Number of minutes. |
Returns
Duration - Duration representing the given minutes
Example
Duration.hours(h)
Create a duration from a number of hours.
Parameters
| Parameter | Type | Description |
|---|---|---|
h | number | bigint | Number of hours. |
Returns
Duration - Duration representing the given hours
Example
Duration.days(d)
Create a duration from a number of days.
Parameters
| Parameter | Type | Description |
|---|---|---|
d | number | bigint | Number of days. |
Returns
Duration - Duration representing the given days
Example
Duration.weeks(w)
Create a duration from a number of weeks.
Parameters
| Parameter | Type | Description |
|---|---|---|
w | number | bigint | Number of weeks. |
Returns
Duration - Duration representing the given weeks
Example
Duration.years(y)
Create a duration from a number of years.
Parameters
| Parameter | Type | Description |
|---|---|---|
y | number | bigint | Number of years. |
Returns
Duration - Duration representing the given years
Example
Duration.parseFloat(input)
Parse a duration from a float string with a unit suffix.
Parameters
| Parameter | Type | Description |
|---|---|---|
input | string | A float string with a unit suffix (e.g., "1.5s"). |
Returns
Duration - Parsed duration
Example
Duration.measure()
Returns a function that, when called, returns the elapsed Duration since the call to Duration.measure().
Returns
() => Duration - A function that returns the elapsed duration
Example
Property Getters
Property getters for accessing the total duration in specific units. All return bigint.
| Property | Type | Description |
|---|---|---|
nanoseconds | bigint | Total nanoseconds |
microseconds | bigint | Total microseconds |
milliseconds | bigint | Total milliseconds |
seconds | bigint | Whole seconds |
minutes | bigint | Total whole minutes |
hours | bigint | Total whole hours |
days | bigint | Total whole days |
weeks | bigint | Total whole weeks |
years | bigint | Total whole years |
Examples
Instance Methods
.toString()
Convert to human-readable string.
Returns
string - Human-readable duration string
Example
.toJSON()
Serialize for JSON.
Returns
string - Duration string for JSON
.toCompact()
Convert to a compact tuple representation.
Returns
[bigint, bigint] | [bigint] | [] - Compact representation: [seconds, nanoseconds], [seconds] if nanoseconds is zero, or [] for a zero duration.
Example
.add(other)
Add another duration.
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Duration | Duration to add. |
Returns
Duration - Sum of durations
Example
.sub(other)
Subtract another duration.
Parameters
| Parameter | Type | Description |
|---|---|---|
other | Duration | Duration to subtract. |
Returns
Duration - Difference of durations
Example
.mul(factor)
Multiply a duration by a scalar.
Parameters
| Parameter | Type | Description |
|---|---|---|
factor | number | bigint | Scalar to multiply by. |
Returns
Duration - Scaled duration
Example
.div(divisor)
Divide a duration. Overloaded: dividing by a Duration returns the ratio as bigint, dividing by a number returns a new Duration.
Parameters
| Parameter | Type | Description |
|---|---|---|
divisor | Duration | number | bigint | Duration for ratio, or scalar for division. |
Returns
bigint when dividing by a Duration, Duration when dividing by a number or bigint.
Examples
.mod(mod)
Get the remainder after dividing by another duration.
Parameters
| Parameter | Type | Description |
|---|---|---|
mod | Duration | Duration to divide by. |
Returns
Duration - Remainder after division
Example
.equals(other)
Check if two durations are equal.
Returns
boolean - True if equal
Complete Examples
Timeouts and Expiration
Query Timeouts
Rate Limiting
Scheduled Tasks
Cache TTL
Performance Measurement
Conversion Examples
Complex Durations
Static Factory Methods
Best Practices
1. Use Human-Readable Formats
2. Use Duration for Time Arithmetic
3. Store Durations in Database
4. Use Appropriate Units
5. Use Duration.measure() for Timing
See Also
DateTime - Datetime values
Data Types Overview - All custom data types
Query Builders - Using Duration in queries
SurrealQL Duration - Database duration type