Skip to main content

Date

A date variable is a representation of a Date or a Date and Time in the UTC timezone

Construction

You can construct a date using a constructor function

// Creates a new date variable with the current date with no time
today = Date()

// Creates a new date variable for the 8th October 2020
date = Date("2020-10-08")

// Creates a new date variable for the 8th October 2020 at 12:46
datetime = Date("2020-10-08T12:46:00")

/**
Creates a new date variable using the start of the delivery period
for October 2020 (2020M10) i.e. 1st October 2020
**/
datepc = Date("2020M10")

// Creates a new date variable for the 8th October 2020 using a different format
df = Date("06/10/2020", "dd/MM/yyyy")

You can also use the [date active variable](https://opendatadsl.atlassian.net/wiki/spaces/DOCUMENTAT/pages/32971/Date+Service) to construct a date, e.g.

// Creates a new date variable for the next Monday (or today if today is Monday)
dav = ${date:"monday"}

Properties

A date has the following properties:

NameDescriptionType
dowThe name of the day of the week, e.g. MondayScalar(String)
dayThe day of the monthScalar(Integer)
monthThe month of the year (1-12)Scalar(Integer)
monthNameThe name of the month, e.g. JanuaryScalar(String)
yearThe yearScalar(Integer)
hourThe hour of the day (0-23)Scalar(Integer)
minuteThe minute of the hour (0-59)Scalar(Integer)
secondThe second of the minute (0-59)Scalar(Integer)

Example usage of properties:

// Creates a new date variable for the 8th October 2020 at 12:46
datetime = Date("2020-10-08T12:46:00")
print datetime.hour
print datetime.minute
print datetime.second

Methods

The date variable has the following methods:

NameDescriptionReturns
next()Returns the next calendar day after this oneDate
next(Calendar)Returns the next day after this one using the supplied calendarDate
previous()Returns the previous calendar day before this oneDate
previous(Calendar)Returns the previous day before this one using the calendarDate
startOfMonth()Returns a new Date as the start of the monthDate
startOfMonth(Calendar)Returns a new Date as the start of the month using the supplied calendarDate
endOfMonth()Returns a new Date as the end of the monthDate
endOfMonth(Calendar)Returns a new Date as the end of the month using the supplied calendarDate
dayOfMonth(day)Returns a new Date as the specific day of the monthDate
dayOfMonth(day, Calendar)Returns a new Date as the specific day of the month using the supplied calendarDate
isHoliday(Calendar)Returns true is this date is a holiday in the supplied calendarScalar(Boolean)

Example using methods:

// Create a date for the 5th October 2020
date5oct = Date("2020-10-05")

// Get the previous calendar day
date4oct = date5oct.previous()

/**
Checks if the 4th October 2020 is a holiday according to the
business (Monday to Friday) calendar
**/
print date4oct.isHoliday("business")

// Prints out the previous date according to the business calendar
print date5oct.previous("business")

Operations

Dates can be used in mathematical operations, for example subtracting one date from another produces a Duration variable. Here are a list of valid date operations:

Subtraction

Subtracting one date from another creates a duration, e.g.

d1 = Date("2020-06-08")
d2 = ${date:"2020-06-09"}

// Test for date subtraction
dur1D = d2 - d1
assertEquals("1D", dur1D)

Subtracting a number from a date produces a new date with that number of days subtracted, e.g.

// Test for date and numeric subtraction
datesub1 = Date("2020-10-08") - 1
assertEquals("2020-10-07T00:00:00", datesub1)

Subtracting a duration from a date produces a new date with the duration subtracted from it, e.g.

// Test for date and duration subtraction
datesub2 = Date("2020-10-08") - 1M1D
assertEquals("2020-09-07T00:00:00", datesub2)

Addition

Adding a duration to a date creates a new date, e.g.

// Test for date and duration addition
datedur = Date("2020-10-08") + 1D
assertEquals("2020-10-09T00:00:00", datedur)

Adding a number to a date creates a new date with that number of days added, e.g.

// Test for date and numeric addition
datenum = Date("2020-10-08") + 5
assertEquals("2020-10-13T00:00:00", datenum)

More Examples

Start of the month

//#region Set start of month
d1 = Date("2023-10-15")
d2 = d1.startOfMonth()
print d2

d1 = Date("2023-10-15")
d2 = d1.startOfMonth("BUSINESS")
print d2
//#endregion

End of the month

//#region Set end of month
d1 = Date("2024-03-15")
d2 = d1.endOfMonth()
print d2

d1 = Date("2024-03-15")
d2 = d1.endOfMonth("BUSINESS")
print d2
//#endregion

Day of the month

//#region Day of the month
d1 = Date("2024-05-15")
d2 = d1.dayOfMonth(11)
print d2

d1 = Date("2024-05-15")
d2 = d1.dayOfMonth(11, "BUSINESS")
print d2
//#endregion