Date Functions
When you work with a date, you may use functions that process dates.
In these functions, sometimes a format pattern of a date or any number must be defined. Also a locale and time zone can have an influence on their formatting.
- For detailed information about the date formatting and/or parsing, see Date and Time Format.
- For detailed information about locale, see Locale.
- For detailed information about time zones, see Time Zone.
Remember that numeric and date formats are displayed using the system value Locale or Locale specified in the defaultProperties file, unless other Locale is explicitly specified. Similarly for Time zone.
For more information on how Locale and Time zone may be changed in the defaultProperties, see Engine Configuration.
Here we provide the list of the functions.
createDate
date createDate( integer year, integer month, integer day );
date createDate( integer year, integer month, integer day, string timeZone );
date createDate( integer year, integer month, integer day, integer hour, integer minute, integer second );
date createDate( integer year, integer month, integer day, integer hour, integer minute, integer second, string timeZone );
date createDate( integer year, integer month, integer day, integer hour, integer minute, integer second, integer millisecond );
date createDate( integer year, integer month, integer day, integer hour, integer minute, integer second, integer millisecond, string timeZone );
The function createDate()
creates a date using provided year, month (numbered from 1), day of month, hour, minute, second, millisecond and time zone.
If any of the above mentioned parameters is missing, value is set to zero. If the time zone is missing, the default time zone is used.
If one or more of specified parameters is null
, the function fails.
Example 124. Usage of createDate
- The function
createDate(2013, 7, 31)
returns a date corresponding to the 31 July 2013 0:00 using the default time zone. The summer/winter time is taken into account. For example, the expression returns30 July 2013 22:00 GMT
in time zoneGMT+1
using the summer time. - The function
createDate(2013, 10, 4, "GMT+3")
returns4 October 2013 0:00 GMT+3
. It is the same as3 October 2013 21:00 GMT+0
. - The function
createDate(2009, 2, 13, 23, 31, 30)
returns13 February 2009 23:31:30
in the default time zone. For example the expression corresponds to13 February 2009 22:31:30 GMT
if the default time zone isGMT+1
. - The function
createDate(2009, 2, 13, 23, 31, 30, "GMT-1")
returns14 February 2009 0:31:30 GMT
. - The function
createDate(2009, 2, 13, 23, 31, 30, 123)
returns13 February 2009 23:31:30.123
in the default time zone. For example the expression corresponds to13 February 2009 22:31:30.123 GMT
if the default time zone isGMT+1
. - The function
createDate(2009, 2, 13, 23, 31, 30, 124, "GMT-1")
returns14 February 2009 0:31:30.124 GMT
.
See also: str2date
dateAdd
date dateAdd( date arg, long amount, unit timeunit );
The dateAdd() function adds a number of time units to the date and returns a new date.
Parameter arg
is the date to which the number of time units is added.
Parameter amount
defines the number of units to be added.
The unit
parameter defines the unit of the second function parameter. The unit
argument can be one of the following: year, month, week, day, hour, minute, second, millisec
. The unit must be specified as a constant. It can neither be received through an edge nor set as a variable.
The function returns the new resulting date.
If one of the arguments is null
, the function fails with an error.
Example 125. Usage of dateAdd
- Let us set up
date d
to 13 February 2009 23:31:30 GMT. - The function
dateAdd(d, 1, year)
returns2010-02-13 23:31:30 GMT
. - The function
dateAdd(d, 1, month)
returns2009-03-13 23:31:30 GMT
. - The function
dateAdd(d, 1, day)
returns2009-02-14 23:31:30 GMT
. - The
czDate
is30.3.2014 00:00:00
in the time zoneEurope/Prague
. - The function
dateAdd(czDate, 24, hour)
returns31.3.2014 01:00:00 CEST
. - The function
dateAdd(czDate, 1, day)
returns31.3.2014 00:00:00 CEST
- The
ukDate
is2014-03-30 00:20:00
in the time zoneEurope/London
. - The function
dateAdd(ukDate, 41, minute)
returns2014-04 02:01:00
in the time zoneEurope/London
. - The function
dateAdd(ukDate, 1, hour)
returns2014-03-30 02:20:00
in the time zoneEurope/London
. - The function
dateAdd(ukDate, 1, day)
returns2014-03-31 00:20:00
in the time zoneEurope/London
.
See also: createDate, dateDiff
dateDiff
long dateDiff( date later, date earlier, unit timeunit );
The dateDiff()
function returns the difference of two date variables in a specified time units.
The later
and earlier
parameters represent a later and earlier date respectively.
The difference of two dates is expressed in defined time units. The unit
can be one of the following: year, month, week, day, hour, minute, second, millisec
. The unit must be specified as a constant. It can be neither received through an edge nor set as variable.
If the unit is hour, minute, second or millisec, it counts the difference in the same way as it would be measured using stopclock. If the unit is day, month or year, the difference affected by winter/summer time turn is calculated differently. See the examples below!
The result is a long
number. The result of the function is truncated: two date variables with a difference of 40
hours yield a difference of 1
days.
If one of the given argument is null
, the function fails with an error.
Example 126. Usage of dateDiff
- The function
dateDiff(2008-06-18, 2001-02-03, year)
returns7
. But,dateDiff(2001-02-03, 2008-06-18, year)
returns-7
. - Let’s call
2009-02-13 23:31:30 GMT+0
asd1
and2011-01-10 20:12:33
asd2
. - The function
dateDiff(d2, d1, year)
returns1
. - The function
dateDiff(d2, d1, month)
returns22
. - The function
dateDiff(d2, d1, day)
returns695
. - The variable
L2
is2014-03-30 02:01:00
in the time zoneEurope/London
. The variableL1
is2014-03-30 00:59:00
in the time zoneEurope/London
. The functiondateDiff(L2, L1, minute)
returns2
. - Set
2014-03-30 00:15:00
with the time zoneEurope/London
aslondon1
and set2014-03-30 02:15:00
with the same time zone aslondon2
. The functiondateDiff(london2, london1, hour)
returns1
. - Set
2014-03-30 00:15:00
with the time zoneAmerica/New_York
asny1
and set2014-03-30 02:15:00
with the same time zone asny2
. The functiondateDiff(ny2, ny1, hour)
returns2
. - Set
2014-02-10 10:15:00
with the time zoneAmerica/New_York
asnyFeb10
and2014-02-10 10:15:00
with the time zoneEurope/London
asukFeb10
. The functiondateDiff(nyFeb10, ukFeb10, hour)
returns5
. - Set
2014-03-10 10:15:00
with the time zoneAmerica/New_York
asnyMar10
and2014-03-10 10:15:00
with the time zoneEurope/London
asukMar10
. The functiondateDiff(nyMar10, ukMar10, hour)
returns4
. - Set
2014-03-08 12:14:16
with the time zoneAmerica/New_York
asny21
and2014-03-09 12:14:16
with the same time zone asny22
. - The function
dateDiff(ny22, ny21, millisec)
returns82800000
. - The function
dateDiff(ny22, ny21, second)
returns82800
. - The function
dateDiff(ny22, ny21, minute)
returns1380
. - The function
dateDiff(ny22, ny21, hour)
returns23
. - The function
dateDiff(ny22, ny21, day)
returns1
if processing runs on machine with the same time zone as the data (America/New_York
). The function returns0
if processing runs on machine with a different time zone (e.g.Europe/London
). Time in the time zoneEurope/London
is turned on the different day than in time zones in US. If you would process data having the time zoneAmerica/New_York
using the time zoneAmerica/Los_Angeles
, you would get0
or1
.
extractDate
date extractDate( date arg );
The extractDate
function takes a date argument and returns only the information containing year, month, and day values. The function’s argument is not modified by the return value.
If the input argument is null
, the function returns null
.
The default locale and default time zone are applied.
Example 127. Usage of extractDate
- Let’s call 13 February 2009 23:31:30 GMT+0 as d.
- The function
extractDate(d)
returns2009-02-13 0:00:00 GMT+0
provided the default time zone is GMT+0. If the default time zone is GMT+1, the function will return2009-02-14 0:00:00 GMT+1
. (The result corresponds to2009-02-13 23:00:00 GMT+0
.)
See also: extractTime, str2date
extractTime
date extractTime( date arg );
The extractTime()
function takes a date argument and returns only the information containing hours, minutes, seconds, and milliseconds. The function’s argument is not modified by the return value.
If the input argument is null
, the function returns null
.
The default locale and default time zone are applied.
Example 128. Usage of extractTime
- Let’s call 13 February 2009 23:31:30 GMT+0 as
d
. - The function
extractTime(d)
returns23:31:30
provided the default time zone is GMT+0. If the default time zone is GMT+1, the function will return0:31:30
.
See also: extractDate, str2date
getYear
integer getYear( date arg );
integer getYear( date arg, string timeZone );
The getYear()
function returns the year of arg
.
If the argument is null
, the function returns null
.
If the timeZone
argument is null
or the argument is missing, the function uses the default Time Zone.
Example 129. Usage of getYear
- Let’s call 2011-01-01 1:05:00 GMT as
d
. - The function
getYear(d)
returns2011
. The default time zone is used. - The function
getYear(d, "GMT+0")
returns2011
. - the function
getYear(d, "GMT-3")
returns2010
. There have not been a midnight in theGMT-3
yet.
See also: date2str, getMonth, getDay, getHour, getMinute, getSecond, getMillisecond, str2date
getMonth
integer getMonth( date arg );
integer getMonth( date arg, string timeZone );
The getMonth()
function returns the month of the year (numbered from 1) of arg
.
If the argument is null
, the function returns null
.
If the timeZone
argument is null
or the argument is missing, the function uses the default Time Zone.
Example 130. Usage of getMonth
- Let’s call 2011-01-01 1:05:00 GMT as
d
. - The function
getMonth(d)
returns1
provided the default time zone isGMT+1
. - The function
getMonth(d, "GMT+0")
returns1
. - The function
getMonth(d, "GMT-3")
returns12
. There have not been a midnight in theGMT-3
yet.
See also: date2str, getYear, getDay, getHour, getMinute, getSecond, getMillisecond, str2date
getDay
integer getDay( date arg );
integer getDay( date arg, string timeZone );
The getDay()
function returns the day of the month of arg
.
If the argument is null
, the function returns null
.
If the timeZone
argument is null
or the time zone argument is missing, the function uses the default Time Zone.
Example 131. Usage of getDay
- Let’s call 2011-01-01 1:05:00 GMT as
d
. - The function
getDay(d)
returns1
provided the default time zone isGMT+1
. - The function
getDay(d, "GMT+0")
returns1
. - The function
getDay(d, "GMT-3")
returns31
. There have not been a midnight in theGMT-3 yet
.
See also: date2str, getYear, getMonth, getHour, getMinute, getSecond, getMillisecond, str2date
getHour
integer getHour( date arg );
integer getHour( date arg, string timeZone );
The getHour()
function returns the hour of the day (24-hour clock) of arg
.
If the argument is null
, the function returns null
. Otherwise the specified time zone is used.
If the timeZone
argument is null
, the function uses the default Time Zone.
Example 132. Usage of getHour
- Let’s call 2011-01-01 1:05:00 GMT as
d
. - The function
getHour(d)
returns2
provided the default time zone isGMT+1
. - The function
getHour(d, "GMT+0")
returns1
. - The function
getHour(d, "GMT-3")
returns22
.
See also: date2str, getYear, getMonth, getDay, getMinute, getSecond, getMillisecond, str2date
getMinute
integer getMinute( date arg );
integer getMinute( date arg, string timeZone );
The getMinute()
function returns the minute of the hour of arg
.
If the argument is null
, the function returns null
.
If the timeZone
argument is null
or the parameter is not present, the function uses the default Time Zone.
Example 133. Usage of getMinute
- Let’s call 2011-01-01 1:05:00 GMT as
d
. - The function
getMinute(d)
returns5
, provided the default time zone isGMT+1
. - The function
getMinute(d, "GMT+0")
returns5
. - The function
getMinute(d, "GMT-9:30")
returns35
.
See also: date2str, getYear, getMonth, getDay, getHour, getSecond, getMillisecond, str2date
getSecond
integer getSecond( date arg );
integer getSecond( date arg, string timeZone );
The getSecond()
function returns the second of the minute of arg
.
If the argument is null
, the function returns null
.
If the timeZone
argument is null
or the argument is not present, the function uses the default Time Zone.
Example 134. Usage of getSecond
- Let’s call 2011-01-01 1:05:02 GMT as
d
. - The function
getSecond(d)
returns2
. - The function
getSecond(d, "GMT+0")
returns2
. - The function
getSecond(d, "GMT-4")
returns2
.
See also: date2str, getYear, getMonth, getDay, getHour, getMinute, getMillisecond, str2date
getMillisecond
integer getMillisecond( date arg );
integer getMillisecond( date arg, string timeZone );
The getMillisecond()
function returns the millisecond of the second of arg
.
If the argument is null
, the function returns null
.
If the timeZone
argument is null
or the parameter is not present, the function uses the default Time Zone.
Example 135. Usage of getMillisecond
- Let’s call 2011-01-01 1:05:02.123 GMT as
d
. - The function
getMillisecond(d)
returns123
. - The function
getMillisecond(d, "GMT+0")
returns123
. - The function
getMillisecond(d, "GMT-4")
returns123
.
See also: date2str, getYear, getMonth, getDay, getHour, getMinute, getSecond, str2date
randomDate
date randomDate( date startDate, date endDate );
date randomDate( long startDate, long endDate );
date randomDate( string startDate, string endDate, string format );
date randomDate( string startDate, string endDate, string format, string locale );
date randomDate( string startDate, string endDate, string format, string locale, string timeZone );
The randomDate()
function returns a random date between startDate
and endDate
.
These resulting dates are generated at random for different records and different fields. They can be different for both records and fields. The return value can also be startDate
or endDate
. However, it cannot be the date before startDate
nor after endDate
.
If one of the given dates is null
, the function fails with an error.
If the format
is null
or the function does not have the format
parameter, the default value is used.
If the timezone
is null
or the function does not have the timezone
parameter, the default Time Zone is used.
If the locale
is null
or the field is missing, the default Locale is used.
Example 136. Usage of randomDate
- Let’s call
2011-01-01 0:00:00
asdate1
and2012-01-01 0:00:00
asdate2
. The functionrandomDate(date1, date2)
returns for example2011-06-19
. - The function
randomDate(123456789000L, 1266103890000L)
returns for example2009-06-20
. - The function
randomDate("2011-11-11", "2012-12-12", "yyyy-MM-dd")
returns for example2012-09-01
. - The function
randomDate("10 octobre 2011", "11 novembre 2011", "dd MMMM yyyy", "fr.FR")
returns for example2011-10-14
. - The function
randomDate("2011-01-11", "2011-08-12", "yyyy-MM-dd", "en.US", "GMT-5")
returns for example2011-05-14
.
See also: random, randomBoolean, randomGaussian, randomInteger, randomLong, randomString, randomUUID, setRandomSeed
today
date today( );
The today()
function accepts no argument and returns current date and time.
Example 137. Usage of today
The today()
function returns, for example, 2013-11-06 12:32:15
provided today is 6 November 2013 and the time is 12:32:15.
See also: zeroDate
zeroDate
date zeroDate( );
The zeroDate()
function accepts no argument and returns 1.1.1970 0:00:00 GMT.
See also: today
trunc
The function trunc
is deprecated. It returns a value and modifies the argument at the same time.
truncDate
The function truncDate
is deprecated. It returns a value and modifies the argument at the same time.
Updated about 1 year ago