datetime

This library implements a DateTime object that contains all date and time fields as well as functions to manipulate the data. In addition, the library itself contains the following functions:

now()

Returns the DateTime object that represents the current date-time from the system clock in the default time-zone.

Example

ds.datetime.now()
Result:
{
  "zoneOffset": "-07:00",
  "year": 2022,
  "nano": 478000000,
  "minute": 9,
  "second": 34,
  "hour": 9,
  "month": 11,
  "zoneId": "America/Phoenix",
  "day": 18
}

fromObject(object datetime)

Creates a new DateTime object from values provided in the input object. The input object must contain at least one of the day, month, year, hour, minute, second, nano, zoneId or zoneOffset keys.

Example

ds.datetime.fromObject({"year":2020})
Result:
{
  "zoneOffset": "Z",
  "year": 2020,
  "nano": 0,
  "minute": 0,
  "second": 0,
  "hour": 0,
  "month": 1,
  "zoneId": "Z",
  "day": 1
}

parse(string datetime, string inputformat = "iso")

Creates a new DateTime object from a string representation of date-time in a given format. The optional inputformat string may be one of the timestamp, epoch, iso or contain a pattern accepted by the java.time.format.DateTimeFormatter.ofPattern() method. If the input format is not specified, the datetime parameter is expected to be in the java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME format

Example

ds.datetime.parse("12/31/1990 10:10:10", "MM/dd/yyyy HH:mm:ss")
Result:
{
  "zoneOffset": "-07:00",
  "year": 1990,
  "nano": 0,
  "minute": 10,
  "second": 10,
  "hour": 10,
  "month": 12,
  "zoneId": "America/Phoenix",
  "day": 31
}
ds.datetime.parse("1577836800", "timestamp")
Result:
{
  "zoneOffset": "Z",
  "year": 2020,
  "nano": 0,
  "minute": 0,
  "second": 0,
  "hour": 0,
  "month": 1,
  "zoneId": "Z",
  "day": 1
}
ds.datetime.parse("2011-12-03T10:15:30+01:00")
Result:
{
  "zoneOffset": "+01:00",
  "year": 2011,
  "nano": 0,
  "minute": 15,
  "second": 30,
  "hour": 10,
  "month": 12,
  "zoneId": "+01:00",
  "day": 3
}

DateTime object

The DateTime object contains following functions:

asMilliseconds()

Returns the date-time value as the number of milliseconds from the epoch of 1970-01-01T00:00:00Z.

Example

ds.datetime.now().asMilliseconds()
Result:
"1668789455520"

format(string pattern)

Returns the date-time value as a string formatted using the provided pattern accepted by the java.time.format.DateTimeFormatter.ofPattern() method.

Example

ds.datetime.now().format("MM/dd/yyyy HH:mm:ss")
Result:
"11/18/2022 10:23:12"

toISO()

Returns the date-time value as a string formatted using the ISO-8601 extended offset date-time format.

Example

ds.datetime.now().toISO()
Result:
"2022-11-18T11:44:11.571-07:00"

toTimeZone(string zoneId)

Returns a copy of this DateTime object with a different time-zone, retaining the instant.

Example

ds.datetime.parse("2019-07-04T21:00:00Z", "yyyy-MM-dd'T'HH:mm:ssXXX").toTimeZone("America/Los_Angeles")
Result:
{
  "zoneOffset": "-07:00",
  "year": 2019,
  "nano": 0,
  "minute": 0,
  "second": 0,
  "hour": 14,
  "month": 7,
  "zoneId": "America/Los_Angeles",
  "day": 4
}

plus(string period)

Adds a period type to the given datetime. Both Period and Duration patterns are accepted.

Example

ds.datetime.parse("2019-07-22", "yyyy-MM-dd").plus("P5D")
Result
{
  "zoneOffset": "-07:00",
  "year": 2019,
  "nano": 0,
  "minute": 0,
  "second": 0,
  "hour": 0,
  "month": 7,
  "zoneId": "America/Phoenix",
  "day": 27
}

daysBetween(DateTime otherDateTime)

Returns the number of days between this DateTime object and the object provided as a parameter

Example

DataSonnet map:
local myDate = ds.datetime.parse("01/01/1990 10:10:10", "MM/dd/yyyy HH:mm:ss");
local otherDate = ds.datetime.parse("12/31/1990 10:10:10", "MM/dd/yyyy HH:mm:ss");
myDate.daysBetween(otherDate)"
Result
364

compare(DateTime otherDateTime)

Returns the value 0 if the argument DateTime is equal to this DateTime; a value less than 0 if this DateTime is before the DateTime argument; and a value greater than 0 if this DateTime is after the DateTime argument.

Example

DataSonnet map:
local myDate = ds.datetime.parse("01/01/1990 10:10:10", "MM/dd/yyyy HH:mm:ss");
local otherDate = ds.datetime.parse("12/31/1990 10:10:10", "MM/dd/yyyy HH:mm:ss");
myDate.compare(otherDate)"
Result
-1