Duration and Period
Constructors and methods of the Duration and Period objects, which measure and build lengths of time in server scripts.
Duration and Period are globals for lengths of time in server scripts. A Duration is an exact number of milliseconds. A Period is a length in calendar units (years, months, weeks, days, hours, minutes, seconds and milliseconds) that you build and then turn into a Duration.
Get an instance
Call Duration or Period as a function. new is not needed.
let fiveMinutes = Duration("5m");
let elapsed = Duration(DateTime().minusHours(3), DateTime());
let shift = Period().withHours(7).withMinutes(30).toDuration();
| Method | Returns | Description | Since |
|---|---|---|---|
Duration() |
Duration | A duration of 0. | 1.11 |
Duration(millis) |
Duration | A duration of millis milliseconds. A string of digits, such as "1000", is read the same way. |
1.11 |
Duration(text) |
Duration | Parses a number-and-unit string such as "3d2h5m1s". Units are d days, h hours, m minutes, s seconds and S milliseconds. |
1.11 |
Duration(start, end) |
Duration | The time from one DateTime to another. It is negative when end is before start. |
1.11 |
Duration(duration) |
Duration | A copy of another duration. | 1.11 |
Period() |
Period | An empty period. | 1.11 |
Period(duration) |
Period | A period holding the length of a Duration in hours, minutes, seconds and milliseconds. |
1.11 |
Period(start, end) |
Period | A period holding the time from one DateTime to another. |
1.11 |
Duration methods
Every method returns a new Duration, so calls chain and the original never changes. A day is always 24 hours here, whatever the daylight saving rules.
| Method | Returns | Description | Since |
|---|---|---|---|
getStandardDays() |
Number | Whole days. A 36-hour duration gives 1. | 1.11 |
getDays() |
Number | Days with a fraction. A 36-hour duration gives 1.5. | 1.11 |
getStandardHours() |
Number | Whole hours. A 72-minute duration gives 1. | 1.11 |
getHours() |
Number | Hours with a fraction. A 72-minute duration gives 1.2. | 1.11 |
getStandardMinutes() |
Number | Whole minutes. A 72-second duration gives 1. | 1.11 |
getMinutes() |
Number | Minutes with a fraction. A 72-second duration gives 1.2. | 1.11 |
getStandardSeconds() |
Number | Whole seconds. A 2,500-millisecond duration gives 2. | 1.11 |
getSeconds() |
Number | Seconds with a fraction. A 2,500-millisecond duration gives 2.5. | 1.11 |
getMillis() |
Number | The length in milliseconds. | 1.11 |
plus(value), minus(value) |
Duration | Adds or subtracts a Duration, a Period or a number of milliseconds. |
1.11 |
plusDuration(duration), minusDuration(duration) |
Duration | Adds or subtracts a Duration. |
1.11 |
plusDays(days), minusDays(days) |
Duration | Adds or subtracts days of 24 hours. | 1.11 |
plusHours(hours), minusHours(hours) |
Duration | Adds or subtracts hours. | 1.11 |
plusMinutes(minutes), minusMinutes(minutes) |
Duration | Adds or subtracts minutes. | 1.11 |
plusSeconds(seconds), minusSeconds(seconds) |
Duration | Adds or subtracts seconds. | 1.11 |
plusMillis(millis), minusMillis(millis) |
Duration | Adds or subtracts milliseconds. | 1.11 |
toString() |
String | The length in hours, minutes, seconds and milliseconds, for example "27h30m". Whole days show as hours, and units that are 0 are left out. |
1.11 |
In arithmetic or a comparison, a Duration acts as its number of milliseconds.
Period methods
Each with method returns a new Period with that unit set and the others kept.
| Method | Returns | Description | Since |
|---|---|---|---|
withYears(years) |
Period | Sets the years. | 1.11 |
withMonths(months) |
Period | Sets the months. | 1.11 |
withWeeks(weeks) |
Period | Sets the weeks. | 1.11 |
withDays(days) |
Period | Sets the days. | 1.11 |
withHours(hours) |
Period | Sets the hours. | 1.11 |
withMinutes(minutes) |
Period | Sets the minutes. | 1.11 |
withSeconds(seconds) |
Period | Sets the seconds. | 1.11 |
withMillis(millis) |
Period | Sets the milliseconds. | 1.11 |
toDuration() |
Duration | Converts with standard lengths: a week is 7 days and a day is 24 hours. A period with years or months cannot be converted and raises an error. | 1.11 |
Examples
Measure elapsed time
Servicely Script
let start = DateTime([2026, 3, 14, 9, 0]);
let end = DateTime([2026, 3, 15, 12, 30]);
Duration(start, end).getHours();
Response
27.5
Build a duration from units
Servicely Script
Period()
.withDays(5)
.withHours(10)
.withMinutes(25)
.toDuration()
.toString();
Response
"130h25m"
Move a date by a duration
Servicely Script
DateTime([2026, 3, 14, 9, 0])
.plusDuration(Duration("1d6h"))
.format("yyyy-MM-dd HH:mm");
Response
"2026-03-15 15:00"