Date
Overview
Date calculation functions. Includes information about value ranges for months and weeks, as well as several methods for handling dates, such as Date.sysdate, Date.now, and Date.dateSet, along with special notes about month and week value ranges.
Special Notes
Month value ranges
| Month | Value |
|---|---|
| January | 0 |
| February | 1 |
| March | 2 |
| April | 3 |
| May | 4 |
| June | 5 |
| July | 6 |
| August | 7 |
| September | 8 |
| October | 9 |
| November | 10 |
| December | 11 |
day_of_week value ranges
| Day | Value |
|---|---|
| Sunday | 0 |
| Monday | 1 |
| Tuesday | 2 |
| Wednesday | 3 |
| Thursday | 4 |
| Friday | 5 |
| Saturday | 6 |
sysdate
Returns the current date and time
Date.sysdate()Return Value
Type Date Current date and time
Examples
Date.sysdate() //Date objectnow
Returns the current time as a UNIX timestamp
Date.now()Return Value
Type Long Current time as a UNIX timestamp
Examples
Date.now() // 1668483800328newDate
Returns a specified date
Date.newDate(year, month, day, hour, minute, second, millisecond);| Parameter | Type | Description |
|---|---|---|
| year | Integer | Year, optional or can be passed as null, defaults to current year |
| month | Integer | Month, optional or can be passed as null, defaults to January |
| day | Integer | Day, optional or can be passed as null, defaults to 1 |
| hour | Integer | Hour, optional or can be passed as null, defaults to 0 |
| minute | Integer | Minute, optional or can be passed as null, defaults to 0 |
| second | Integer | Second, optional or can be passed as null, defaults to 0 |
| millisecond | Integer | Millisecond, optional or can be passed as null, defaults to 0 |
Return Value
Type Date Returns the specified date
Examples
Date.newDate(); //Returns current date and time if no parameters are passed
Date.newDate(null); //2025-01-01 00:00:00,000
Date.newDate(2022);// 2022-01-01 00:00:00,000
Date.newDate(2025, 0, 3); // 2025-01-03 00:00:00,000
Date.newDate(2025, 0, 1, 2, 3, 4, 5);// 2025-01-01 02:03:04,005
Date.newDate(2025, null, null); // 2025-01-01 00:00:00,000
Date.newDate(null, null, null, null, null, null, null, null); // 2025-01-01 00:00:00,000
Date.newDate(null, 0, 1, 2, 3, 4, 5, 6); // 2025-01-01 02:03:04,005
Date.newDate(null, 0, 1, null, 3, 4, 5); // 2025-01-01 00:03:04,005dateSet
Sets the part specified by type in date d to value
Date.dateSet(d, type, value)| Parameter | Type | Description |
|---|---|---|
| d | Date or Long | Date or UNIX timestamp to calculate from |
| type | String | Operation type |
| value | Integer | Value to set, defaults to 0 if null |
INFO
The values for type are: year, month, day_of_year, day_of_month, day_of_week, hour, minute, second, millisecond
Return Value
Type Date Calculated date
Examples
//Assume date value is 2022-11-01 00:00:00,000, timestamp is 1667232000000
Date.dateSet(date, 'year', 2024); //2024-11-01 00:00:00,000
Date.dateSet(date, 'year', null); //0001-11-01 00:00:00,000
Date.dateSet(date, 'year', -1); //0002-11-01 00:00:00,000
Date.dateSet(null, 'year', 2024); //null
Date.dateSet(date, null, 2024); //null
Date.dateSet(date, 'month', 1); //2022-02-01 00:00:00,000
Date.dateSet(date, 'day_of_year', 1); //2022-01-01 00:00:00,000
Date.dateSet(date, 'day_of_month', 1); //2022-11-01 00:00:00,000
Date.dateSet(date, 'day_of_week', 1); //2022-10-30 00:00:00,000
Date.dateSet(date, 'hour', 1); //2022-11-01 01:00:00,000
Date.dateSet(date, 'minute', 1); //2022-11-01 00:01:00,000
Date.dateSet(date, 'second', 1); //2022-11-01 00:00:01,000
Date.dateSet(date, 'millisecond', 1); //2022-11-01 00:00:00,001
Date.dateSet(timestamp, 'year', 2024); //2024-11-01 00:00:00,000
Date.dateSet(timestamp, 'month', 1); //2022-02-01 00:00:00,000dateAdd
Calculates the date by adding diff of type to date d
Date.dateAdd(d, type, diff)| Parameter | Type | Description |
|---|---|---|
| d | Date or Long | Date or UNIX timestamp to calculate from |
| type | String | Operation type |
| diff | Integer | Value to add or subtract, defaults to 0 if null. Add with positive integer, subtract with negative integer |
INFO
The values for type are: year, month, day_of_year, day_of_month, day_of_week, hour, minute, second, millisecond
Return Value
Type Date Calculated date
Examples
//Assume date value is 2022-11-01 00:00:00,000, timestamp is 1667232000000
Date.dateAdd(date, 'year', 1); //2023-11-01 00:00:00,000
Date.dateAdd(date, 'year', null); //2022-11-01 00:00:00,000
Date.dateAdd(date, 'year', -1); //2021-11-01 00:00:00,000
Date.dateAdd(date, 'month', 1); //2022-12-01 00:00:00,000
Date.dateAdd(date, 'day_of_year', 1); //2022-11-02 00:00:00,000
Date.dateAdd(date, 'day_of_month', 1); //2022-11-02 00:00:00,000
Date.dateAdd(date, 'day_of_week', 1); //2022-11-02 00:00:00,000
Date.dateAdd(date, 'hour', 1); //2022-11-01 01:00:00,000
Date.dateAdd(date, 'minute', 1); //2022-11-01 00:01:00,000
Date.dateAdd(date, 'second', 1); //2022-11-01 00:00:01,000
Date.dateAdd(date, 'millisecond', 1); //2022-11-01 00:00:00,001
Date.dateAdd(timestamp, 'year', 2); //2024-11-01 00:00:00,000
Date.dateAdd(timestamp, 'month', 1); //2022-12-01 00:00:00,000datePart
Returns the part specified by type from date d
Date.datePart(d, type)| Parameter | Type | Description |
|---|---|---|
| d | Date or Long | Date or UNIX timestamp to calculate from |
| type | String | Operation type |
INFO
The values for type are: year, month, day_of_year, day_of_month, day_of_week, hour, minute, second, millisecond
Return Value
Type Integer The part specified by type from date d
Examples
//Assume date value is 2022-11-01 13:10:12,000, timestamp is 1667279412000
Date.datePart(date, 'year'); //2022
Date.datePart(date, 'month'); //10
Date.datePart(date, 'day_of_year'); //305
Date.datePart(date, 'day_of_month'); //1
Date.datePart(date, 'day_of_week'); //3
Date.datePart(date, 'hour'); //13
Date.datePart(date, 'minute'); //10
Date.datePart(date, 'second'); //12
Date.datePart(date, 'millisecond'); //0
Date.datePart(null, 'year'); //null
Date.datePart(date, null); //null
Date.datePart(timestamp, 'year'); //2022
Date.datePart(timestamp, 'month'); //10
Date.datePart(date, 'week_of_year'); //45dateBefore
Determines if date d1 is before date d2
Date.dateBefore(d1, d2)| Parameter | Type | Description |
|---|---|---|
| d1 | Date or Long | Date 1 or UNIX timestamp |
| d2 | Date or Long | Date 2 or UNIX timestamp |
Return Value
Type Boolean Whether date d1 is before date d2, returns false if d1 and d2 are equal
Examples
//Assume date1 value is 2022-11-01 13:10:12,000, timestamp1 is 1667279412000
//Assume date2 value is 2022-02-02 13:10:12,000, timestamp2 is 1643778612000
Date.dateBefore(date1, date2); //false
Date.dateBefore(date2, date1); //true
Date.dateBefore(timestamp1, timestamp2); //false
Date.dateBefore(timestamp2, timestamp1); //true
Date.dateBefore(timestamp1, date2); //false
Date.dateBefore(timestamp2, date1); //true
Date.dateBefore(date1, null); //false
Date.dateBefore(null, date2); //false
Date.dateBefore(timestamp1, null); //false
Date.dateBefore(null, timestamp2); //false
Date.dateBefore(null, null); //falsedateAfter
Determines if date d1 is after date d2
Date.dateAfter(d1, d2)| Parameter | Type | Description |
|---|---|---|
| d1 | Date or Long | Date 1 or UNIX timestamp |
| d2 | Date or Long | Date 2 or UNIX timestamp |
Return Value
Type Boolean Whether date d1 is after date d2, returns false if d1 and d2 are equal
Examples
//Assume date1 value is 2022-11-01 13:10:12,000, timestamp1 is 1667279412000
//Assume date2 value is 2022-02-02 13:10:12,000, timestamp2 is 1643778612000
Date.dateAfter(date1, date2); //true
Date.dateAfter(date2, date1); //false
Date.dateAfter(timestamp1, timestamp2); //true
Date.dateAfter(timestamp2, timestamp1); //false
Date.dateAfter(timestamp1, date2); //true
Date.dateAfter(timestamp2, date1); //false
Date.dateAfter(date1, null); //false
Date.dateAfter(null, date2); //false
Date.dateAfter(timestamp1, null); //false
Date.dateAfter(null, timestamp2); //false
Date.dateAfter(null, null); //falsedateDiff
Calculates the number of days between two dates
Date.dateDiff(d1, d2)| Parameter | Type | Description |
|---|---|---|
| d1 | Date or Long | Date 1 or UNIX timestamp |
| d2 | Date or Long | Date 2 or UNIX timestamp |
Return Value
Type Integer Calculates the number of days between two dates. Returns 0 if equal, negative value if d1 is before d2, positive value if d1 is after d2
Examples
//Assume date1 value is 2022-10-01 13:10:12, timestamp1 is 1667279412000
//Assume date2 value is 2023-12-01 13:10:12, timestamp2 is 1701407412000
Date.dateDiff(date1, date2); //-426
Date.dateDiff(date2, date1); //426
Date.dateDiff(timestamp1, timestamp2); //-426
Date.dateDiff(timestamp2, timestamp1); //426
Date.dateDiff(timestamp1, date2); //-426
Date.dateDiff(timestamp2, date1); //426
Date.dateDiff(date1, null); //null
Date.dateDiff(null, date2); //null
Date.dateDiff(timestamp1, null); //null
Date.dateDiff(null, timestamp2); //null
Date.dateDiff(null, null); //nullmonthDiff
Calculates the number of months between two dates
Date.monthDiff(d1, d2)| Parameter | Type | Description |
|---|---|---|
| d1 | Date or Long | Date 1 or UNIX timestamp |
| d2 | Date or Long | Date 2 or UNIX timestamp |
Return Value
Type Integer Calculates the number of months between two dates. Returns 0 if equal, negative value if d1 is before d2, positive value if d1 is after d2
Examples
//Assume date1 value is 2022-10-01 13:10:12, timestamp1 is 1667279412000
//Assume date2 value is 2023-12-01 13:10:12, timestamp2 is 1701407412000
Date.monthDiff(date1, date2); //-14
Date.monthDiff(date2, date1); //14
Date.monthDiff(timestamp1, timestamp2); //-14
Date.monthDiff(timestamp2, timestamp1); //14
Date.monthDiff(timestamp1, date2); //-14
Date.monthDiff(timestamp2, date1); //14
Date.monthDiff(date1, null); //null
Date.monthDiff(null, date2); //null
Date.monthDiff(timestamp1, null); //null
Date.monthDiff(null, timestamp2); //null
Date.monthDiff(null, null); //nullweekDiff
Calculates the number of weeks between two dates
Date.weekDiff(d1, d2)| Parameter | Type | Description |
|---|---|---|
| d1 | Date or Long | Date 1 or UNIX timestamp |
| d2 | Date or Long | Date 2 or UNIX timestamp |
Return Value
Type Integer Calculates the number of weeks between two dates. Returns 0 if equal, negative value if d1 is before d2, positive value if d1 is after d2
Examples
//Assume date1 value is 2022-10-01 13:10:12, timestamp1 is 1667279412000
//Assume date2 value is 2023-12-01 13:10:12, timestamp2 is 1701407412000
Date.weekDiff(date1, date2); //-60
Date.weekDiff(date2, date1); //60
Date.weekDiff(timestamp1, timestamp2); //-60
Date.weekDiff(timestamp2, timestamp1); //60
Date.weekDiff(timestamp1, date2); //-60
Date.weekDiff(timestamp2, date1); //60
Date.weekDiff(date1, null); //null
Date.weekDiff(null, date2); //null
Date.weekDiff(timestamp1, null); //null
Date.weekDiff(null, timestamp2); //null
Date.weekDiff(null, null); //nullquarterDiff
Calculates the number of quarters between two dates
Date.quarterDiff(d1, d2)| Parameter | Type | Description |
|---|---|---|
| d1 | Date or Long | Date 1 or UNIX timestamp |
| d2 | Date or Long | Date 2 or UNIX timestamp |
Return Value
Type Integer Calculates the number of quarters between two dates. Returns 0 if equal, negative value if d1 is before d2, positive value if d1 is after d2
Examples
//Assume date1 value is 2022-10-01 13:10:12, timestamp1 is 1667279412000
//Assume date2 value is 2023-12-01 13:10:12, timestamp2 is 1701407412000
Date.quarterDiff(date1, date2); //-4
Date.quarterDiff(date2, date1); //4
Date.quarterDiff(timestamp1, timestamp2); //-4
Date.quarterDiff(timestamp2, timestamp1); //4
Date.quarterDiff(timestamp1, date2); //-4
Date.quarterDiff(timestamp2, date1); //4
Date.quarterDiff(date1, null); //null
Date.quarterDiff(null, date2); //null
Date.quarterDiff(timestamp1, null); //null
Date.quarterDiff(null, timestamp2); //null
Date.quarterDiff(null, null); //null
