Skip to content

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

MonthValue
January0
February1
March2
April3
May4
June5
July6
August7
September8
October9
November10
December11

day_of_week value ranges

DayValue
Sunday0
Monday1
Tuesday2
Wednesday3
Thursday4
Friday5
Saturday6

sysdate

Returns the current date and time

javascript
Date.sysdate()

Return Value

Type Date Current date and time

Examples

javascript
Date.sysdate() //Date object

now

Returns the current time as a UNIX timestamp

javascript
Date.now()

Return Value

Type Long Current time as a UNIX timestamp

Examples

javascript
Date.now() // 1668483800328

newDate

Returns a specified date

javascript
Date.newDate(year, month, day, hour, minute, second, millisecond);
ParameterTypeDescription
yearIntegerYear, optional or can be passed as null, defaults to current year
monthIntegerMonth, optional or can be passed as null, defaults to January
dayIntegerDay, optional or can be passed as null, defaults to 1
hourIntegerHour, optional or can be passed as null, defaults to 0
minuteIntegerMinute, optional or can be passed as null, defaults to 0
secondIntegerSecond, optional or can be passed as null, defaults to 0
millisecondIntegerMillisecond, optional or can be passed as null, defaults to 0

Return Value

Type Date Returns the specified date

Examples

javascript
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,005

dateSet

Sets the part specified by type in date d to value

javascript
Date.dateSet(d, type, value)
ParameterTypeDescription
dDate or LongDate or UNIX timestamp to calculate from
typeStringOperation type
valueIntegerValue 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

javascript
//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,000

dateAdd

Calculates the date by adding diff of type to date d

javascript
Date.dateAdd(d, type, diff)
ParameterTypeDescription
dDate or LongDate or UNIX timestamp to calculate from
typeStringOperation type
diffIntegerValue 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

javascript
//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,000

datePart

Returns the part specified by type from date d

javascript
Date.datePart(d, type)
ParameterTypeDescription
dDate or LongDate or UNIX timestamp to calculate from
typeStringOperation 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

javascript
//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'); //45

dateBefore

Determines if date d1 is before date d2

javascript
Date.dateBefore(d1, d2)
ParameterTypeDescription
d1Date or LongDate 1 or UNIX timestamp
d2Date or LongDate 2 or UNIX timestamp

Return Value

Type Boolean Whether date d1 is before date d2, returns false if d1 and d2 are equal

Examples

javascript
//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); //false

dateAfter

Determines if date d1 is after date d2

javascript
Date.dateAfter(d1, d2)
ParameterTypeDescription
d1Date or LongDate 1 or UNIX timestamp
d2Date or LongDate 2 or UNIX timestamp

Return Value

Type Boolean Whether date d1 is after date d2, returns false if d1 and d2 are equal

Examples

javascript
//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); //false

dateDiff

Calculates the number of days between two dates

javascript
Date.dateDiff(d1, d2)
ParameterTypeDescription
d1Date or LongDate 1 or UNIX timestamp
d2Date or LongDate 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

javascript
//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); //null

monthDiff

Calculates the number of months between two dates

javascript
Date.monthDiff(d1, d2)
ParameterTypeDescription
d1Date or LongDate 1 or UNIX timestamp
d2Date or LongDate 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

javascript
//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); //null

weekDiff

Calculates the number of weeks between two dates

javascript
Date.weekDiff(d1, d2)
ParameterTypeDescription
d1Date or LongDate 1 or UNIX timestamp
d2Date or LongDate 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

javascript
//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); //null

quarterDiff

Calculates the number of quarters between two dates

javascript
Date.quarterDiff(d1, d2)
ParameterTypeDescription
d1Date or LongDate 1 or UNIX timestamp
d2Date or LongDate 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

javascript
//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