---
title: "DateTime"
site: api
version: 1.11
versions: "all"
url: https://docs-new.servicely.ai/api/1.11/scripting/datetime/
type: reference
application: platform
last_updated: 2026-10-19
commit: f1879fd7e955
---


`DateTime` is the global for dates and times in server scripts: triggers, jobs, script libraries, workflow scripts and the server script page. It wraps a Joda-Time date and time. Every method that changes a date returns a new `DateTime`, so calls chain and the original never changes.

## Get an instance

Call `DateTime` as a function. `new` is not needed. A new `DateTime` takes the current user's time zone and locale. Without a signed-in user, or when the user has no time zone, it takes the `system.timezone` and `system.locale` application properties.

```javascript
let now = DateTime();
let launch = DateTime([1968, DateTime.APRIL, 6, 18, 25, 32, 678]);
let parsed = DateTime("2026-03-14T09:30:00.000Z");
```

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `DateTime()` | DateTime | The current date and time. | 1.11 |
| `DateTime(millis)` | DateTime | The instant `millis` milliseconds after 1 January 1970 UTC. | 1.11 |
| `DateTime(dateTime)` | DateTime | A copy of another `DateTime`. | 1.11 |
| `DateTime(isoString)` | DateTime | Parses an ISO 8601 string, for example `"2026-03-14T09:30:00.000Z"`. | 1.11 |
| `DateTime(text, pattern)` | DateTime | Parses `text` with a format pattern, for example `DateTime("14/03/2026", "dd/MM/yyyy")`. See [DateTime formatting and time zones](page:scripting/datetime-formatting). | 1.11 |
| `DateTime([year, month, day, hour, minute, second, millis])` | DateTime | Builds a date from an array. Give at least year, month and day. Time elements you leave out are 0. | 1.11 |
| `DateTime(year, month, day, ...)` | DateTime | The same as the array form with two or more numbers as separate arguments. One number on its own is read as milliseconds. | 1.11 |

## Static functions and constants

These are called on the `DateTime` global itself, not on an instance.

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `DateTime.now()` | DateTime | The current date and time, the same as `DateTime()`. | 1.11 |
| `DateTime.withLang(locale)` | Nothing | Sets the locale for every `DateTime` the script creates after the call, for example `"fr"` or `"fr-FR"`. | 1.11 |
| `DateTime.JANUARY` to `DateTime.DECEMBER` | Number | Months, 1 to 12. | 1.11 |
| `DateTime.MONDAY` to `DateTime.SUNDAY` | Number | Days of the week, 1 (Monday) to 7 (Sunday), as ISO 8601 numbers them. | 1.11 |
| `DateTime.AM`, `DateTime.PM` | Number | 0 and 1. | 1.11 |
| `DateTime.BC`, `DateTime.BCE` | Number | 0, the era before year 1. | 1.11 |
| `DateTime.AD`, `DateTime.CE` | Number | 1, the current era. | 1.11 |
| `DateTime.MILLIS_PER_SECOND` | Number | 1,000. | 1.11 |
| `DateTime.MILLIS_PER_MINUTE` | Number | 60,000. | 1.11 |
| `DateTime.MILLIS_PER_HOUR` | Number | 3,600,000. | 1.11 |
| `DateTime.MILLIS_PER_DAY` | Number | 86,400,000 in a typical day. Days with a daylight saving change differ. | 1.11 |
| `DateTime.MILLIS_PER_WEEK` | Number | 604,800,000 in a typical week. | 1.11 |
| `DateTime.SECONDS_PER_MINUTE` | Number | 60. | 1.11 |
| `DateTime.SECONDS_PER_HOUR` | Number | 3,600. | 1.11 |
| `DateTime.SECONDS_PER_DAY` | Number | 86,400 in a typical day. | 1.11 |
| `DateTime.SECONDS_PER_WEEK` | Number | 604,800 in a typical week. | 1.11 |
| `DateTime.MINUTES_PER_HOUR` | Number | 60. | 1.11 |
| `DateTime.MINUTES_PER_DAY` | Number | 1,440 in a typical day. | 1.11 |
| `DateTime.MINUTES_PER_WEEK` | Number | 10,080 in a typical week. | 1.11 |
| `DateTime.HOURS_PER_DAY` | Number | 24 in a typical day. | 1.11 |
| `DateTime.HOURS_PER_WEEK` | Number | 168 in a typical week. | 1.11 |
| `DateTime.DAYS_PER_WEEK` | Number | 7. | 1.11 |

## Methods

### Read the parts of a date

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `getYear()` | Number | The year. | 1.11 |
| `getMonthOfYear()` | Number | The month, 1 for January to 12 for December. | 1.11 |
| `getDayOfMonth()` | Number | The day of the month. | 1.11 |
| `getHourOfDay()` | Number | The hour, 0 to 23. | 1.11 |
| `getMinuteOfHour()` | Number | The minute, 0 to 59. | 1.11 |
| `getSecondOfMinute()` | Number | The second, 0 to 59. | 1.11 |
| `getMillisOfSecond()` | Number | The millisecond, 0 to 999. | 1.11 |
| `getMillis()` | Number | Milliseconds since 1 January 1970 UTC. | 1.11 |
| `getZone()` | String | The time zone ID, for example `"Europe/London"`. | 1.11 |

### Set the parts of a date

Each method returns a new `DateTime` with one part changed and the rest kept.

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `withDate(year, month, day)` | DateTime | Sets the date and keeps the time. | 1.11 |
| `withTime(hour, minute, second)` | DateTime | Sets the time with milliseconds at 0 and keeps the date. | 1.11 |
| `withTime(hour, minute, second, millis)` | DateTime | Sets the time and keeps the date. | 1.11 |
| `withYear(year)` | DateTime | Sets the year. | 1.11 |
| `withMonthOfYear(month)` | DateTime | Sets the month, 1 to 12. | 1.11 |
| `withDayOfMonth(day)` | DateTime | Sets the day of the month. | 1.11 |
| `withHourOfDay(hour)` | DateTime | Sets the hour, 0 to 23. | 1.11 |
| `withMinuteOfHour(minute)` | DateTime | Sets the minute. | 1.11 |
| `withSecondOfMinute(second)` | DateTime | Sets the second. | 1.11 |
| `withMillisOfSecond(millis)` | DateTime | Sets the millisecond. | 1.11 |
| `withMillis(millis)` | DateTime | Sets the whole instant from milliseconds since 1 January 1970 UTC. | 1.11 |
| `withTimeAtStartOfDay()` | DateTime | The first moment of the day in this time zone. | 1.11 |
| `toStartOfDay()` | DateTime | Sets the time to 00:00:00.000. | 1.11 |
| `toEndOfDay()` | DateTime | Sets the time to 23:59:59.999. | 1.11 |
| `truncateHours()` | DateTime | Sets hours, minutes, seconds and milliseconds to 0. | 1.11 |
| `truncateMinutes()` | DateTime | Sets minutes, seconds and milliseconds to 0. | 1.11 |
| `truncateSeconds()` | DateTime | Sets seconds and milliseconds to 0. | 1.11 |
| `truncateMillis()` | DateTime | Sets milliseconds to 0. | 1.11 |
| `withZone(zoneID)` | DateTime | The same instant shown in another time zone, for example `"Europe/Paris"`. | 1.11 |
| `toLocalDateTime(zoneID)` | DateTime | The same wall-clock date and time in another time zone. 10:00 in London becomes 10:00 in Paris. | 1.11 |
| `withLang(locale)` | DateTime | Sets the locale used by `format()` and `fromNow()`. | 1.11 |

### Add and subtract

A negative number moves the other way.

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `plusYears(years)`, `minusYears(years)` | DateTime | Adds or subtracts years. | 1.11 |
| `plusMonths(months)`, `minusMonths(months)` | DateTime | Adds or subtracts months. A day that does not exist in the target month becomes the last day of that month. | 1.11 |
| `plusWeeks(weeks)`, `minusWeeks(weeks)` | DateTime | Adds or subtracts weeks. | 1.11 |
| `plusDays(days)`, `minusDays(days)` | DateTime | Adds or subtracts days. | 1.11 |
| `plusHours(hours)`, `minusHours(hours)` | DateTime | Adds or subtracts hours. | 1.11 |
| `plusMinutes(minutes)`, `minusMinutes(minutes)` | DateTime | Adds or subtracts minutes. | 1.11 |
| `plusSeconds(seconds)`, `minusSeconds(seconds)` | DateTime | Adds or subtracts seconds. | 1.11 |
| `plusMillis(millis)`, `minusMillis(millis)` | DateTime | Adds or subtracts milliseconds. | 1.11 |
| `plusDuration(duration)`, `minusDuration(duration)` | DateTime | Adds or subtracts a `Duration`. See [Duration and Period](page:scripting/duration-and-period). | 1.11 |

### Compare

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `isEqual(other)` | Boolean | True when both are the same instant, whatever their time zones. | 1.11 |
| `isBefore(other)` | Boolean | True when this is strictly before `other`. | 1.11 |
| `isAfter(other)` | Boolean | True when this is strictly after `other`. | 1.11 |
| `isBeforeOrEqualTo(other)` | Boolean | True when this is before or the same instant as `other`. | 1.11 |
| `isAfterOrEqualTo(other)` | Boolean | True when this is after or the same instant as `other`. | 1.11 |
| `isSameDay(other)` | Boolean | True when both fall on the same day, whatever the time. | 1.11 |

### Convert and output

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `format()` | String | The ISO 8601 form with milliseconds and offset, for example `"1968-04-06T18:25:32.678+1000"`. | 1.11 |
| `format(pattern)` | String | Formats with a pattern or a style token. See [DateTime formatting and time zones](page:scripting/datetime-formatting). | 1.11 |
| `toISOFormat()` | String | The same as `format()`. | 1.11 |
| `toISOFormatGMT()` | String | The ISO 8601 form in UTC, ending in `Z`, for example `"1968-04-06T08:25:32.678Z"`. | 1.11 |
| `toString()` | String | The same as `toISOFormat()`. Logging or serialising a `DateTime` to JSON gives this form too. | 1.11 |
| `fromNow()` | String | The time from now in words in the object's locale, for example "3 days ago". | 1.11 |
| `toLocalDate()` | LocalDate | The calendar date as seen in this object's own time zone. Use it to set a Date field. | 1.11 |

## Examples

### Build a date and format it

<CodeTabs>
  Servicely Script

  ```javascript
  DateTime([1968, DateTime.APRIL, 6, 18, 25, 32, 678])
      .format("yyyy-MM-dd HH:mm:ss.SSS");
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```javascript
  "1968-04-06 18:25:32.678"
  ```
</CodeTabs>

### Add a month at the end of a month

<CodeTabs>
  Servicely Script

  ```javascript
  DateTime([2026, DateTime.JANUARY, 31, 9, 0])
      .plusMonths(1)
      .format("yyyy-MM-dd");
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```javascript
  "2026-02-28"
  ```
</CodeTabs>

### Move to another time zone

`withZone()` keeps the instant and changes the clock time. `toLocalDateTime()` keeps the clock time and changes the instant.

<CodeTabs>
  Servicely Script

  ```javascript
  let d = DateTime("2020-01-01T10:00:00.000Z").withZone("UTC");

  [
      d.withZone("Europe/Paris").format("yyyy-MM-dd HH:mm"),
      d.toLocalDateTime("Europe/Paris").format("yyyy-MM-dd HH:mm")
  ];
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```javascript
  ["2020-01-01 11:00", "2020-01-01 10:00"]
  ```
</CodeTabs>

### Check a deadline

<CodeTabs>
  Servicely Script

  ```javascript
  let due = DateTime().plusDays(3).toEndOfDay();

  DateTime().isBefore(due);
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```javascript
  true
  ```
</CodeTabs>

### Set a Date field

Take the date from the `DateTime` with `toLocalDate()`. Building a date from `getMillis()` reads the day in the server's time zone, which gives the wrong day for a user in another zone.

```javascript
// <DateField> is a Date field on the current record
current.<DateField>(DateTime().plusDays(7).toLocalDate());
```

## Related

- [DateTime formatting and time zones](page:scripting/datetime-formatting)
- [Duration and Period](page:scripting/duration-and-period)
- [Set the instance and user time zones](page:admin/platform/time-zones)
- [Where scripts run](page:admin/automation/scripting-overview)
