-
Notifications
You must be signed in to change notification settings - Fork 6
Date and Time Functions (DateUtils.xml)
This provides some additional functions for working with Dates and to a certain extent Times.
Unless specified, the functions listed below will be added to a new Date category.
Unit tests for all these functions in DateUtils.Test folder.
Syntax: MAKEDATE(Year, Month, Day)
Examples:
-
MAKEDATE(2010, 1, 2)
returns'2010-01-02'
Creates a date from numerical inputs of Year
, Month
and Day
.
- Please note no sanity checking of the numerical inputs.
Added to Time
category
Syntax: MAKETIME(Hour, Minute, Second)
Examples:
-
MAKETIME(12, 34, 56)
returns'12:34:56'
Creates a time from numerical inputs of Hour
, Minute
and Second
.
- Please note no sanity checking of the numerical inputs.
Added to DateTime
category
Syntax: MAKEDATETIME(Year, Month, Day, Hour, Minute, Second)
Examples:
-
MAKEDATETIME(2010, 1, 2, 12, 34, 56)
returns'2010-01-02 12:34:56'
Creates a date and time from numerical inputs of Year
, Month
, Day
, Hour
, Minute
and Second
.
- Please note no sanity checking of the numerical inputs.
Syntax: TODATE(DateTime)
Examples:
-
TODATE('2010-12-31')
returns2010-12-31
-
TODATE('2010-12-31 11:22:33')
returns2010-12-31
Truncates a DateTime
value to a Date
- Only sanity checking is on the length of the field past. If not 10 (a Date) or 19 (a DateTime), returns
NULL
.
Syntax: TODATETIME(Date)
Examples:
-
TODATETIME('2010-12-31')
returns2010-12-31 00:00:00
-
TODATETIME('2010-12-31 11:22:33')
returns2010-12-31 11:22:33
Appends Midnight to a Date
to create a DateTime
value
- Only sanity checking is on the length of the field past. If not 10 (a Date) or 19 (a DateTime), returns
NULL
.
Syntax: DATEFROMMDY(MDYString)
Examples:
-
DATEFROMMDY('7/2/17')
returns2017-02-07
Parse a string in Month Day Year format to a Date (copes without leading 0s and different separators)
Syntax: DATEFROMDMY(DMYString)
Examples:
-
DATEFROMDMY('7/2/17')
returns2017-07-02
Parse a string in Day Month Year format to a Date (copes without leading 0s and different separators)
Syntax: DAY(DateTime)
Examples:
-
DAY('2010-12-31')
returns31
-
DAY('2010-12-31 11:22:33')
returns31
Returns the day of the month as a number (1 - 31).
Syntax: MONTH(DateTime)
Examples:
-
MONTH('2010-12-31')
returns12
-
MONTH('2010-12-31 11:22:33')
returns12
Returns the month as a number (1 - 12).
Syntax: YEAR(DateTime)
Examples:
-
YEAR('2010-12-31')
returns2010
-
YEAR('2010-12-31 11:22:33')
returns2010
Returns the four digit year as number.
Syntax: WEEKDAY(DateTime)
Examples:
-
WEEKDAY('2010-12-31')
returns5
(Friday) -
WEEKDAY('2010-12-31 11:22:33')
returns5
(Friday) -
WEEKDAY('2012-01-01')
returns0
(Sunday)
Returns the day of the week represented as a number: 0 - Sunday through to 6 - Saturday.
Syntax: QUARTER(DateTime)
Examples:
-
QUARTER('2010-12-31')
returns4
-
QUARTER('2010-12-31 11:22:33')
returns4
-
QUARTER('2010-01-31')
returns1
Returns the quarter of the date as a number (1 - 4).
Syntax: ORDINALDAY(DateTime)
Examples:
-
ORDINALDAY('2010-12-31')
returns31
-
ORDINALDAY('2010-12-31 11:22:33')
returns31
Returns the day of the year as a number (1 - 366).
Syntax: WEEKSTART(DateTime)
Examples:
-
WEEKSTART('2010-12-31')
returns2010-12-26
-
WEEKSTART('2010-12-31 11:22:33')
returns2010-12-26
-
WEEKSTART('2012-01-01')
returns2012-01-01
Returns the nearest Sunday less than or equal to DateTime
.
Syntax: WEEKEND(DateTime)
Examples:
-
WEEKEND('2010-12-31')
returns2011-01-01
-
WEEKEND('2010-12-31 11:22:33')
returns2011-01-01
-
WEEKEND('2012-01-01')
returns2012-01-07
Returns the nearest Saturday greater than or equal to DateTime
.
Syntax: MONTHSTART(DateTime)
Examples:
-
MONTHSTART('2010-12-31')
returns2010-12-01
-
MONTHSTART('2010-12-31 11:22:33')
returns2010-12-01
-
MONTHSTART('2012-01-01')
returns2012-01-01
Returns the first day of the month of DateTime
.
Syntax: MONTHEND(DateTime)
Examples:
-
MONTHEND('2010-12-31')
returns2010-12-01
-
MONTHEND('2010-12-31 11:22:33')
returns2010-12-01
-
MONTHEND('2012-01-01')
returns2012-01-01
Returns the last day of the month of DateTime
.
Syntax: QUARTERSTART(DateTime)
Examples:
-
QUARTERSTART('2010-12-31')
returns2010-01-01
-
QUARTERSTART('2010-12-31 11:22:33')
returns2010-01-01
-
QUARTERSTART('2012-01-01')
returns2012-01-01
Returns the first day of the quarter of DateTime
.
Syntax: QUARTEREND(DateTime)
Examples:
-
QUARTEREND('2010-12-31')
returns2010-12-31
-
QUARTEREND('2010-12-31 11:22:33')
returns2010-12-31
-
QUARTEREND('2012-01-01')
returns2012-12-31
Returns the last day of the quarter of DateTime
.
Syntax: YEARSTART(DateTime)
Examples:
-
YEARSTART('2010-12-31')
returns2010-01-01
-
YEARSTART('2010-12-31 11:22:33')
returns2010-01-01
-
YEARSTART('2012-01-01')
returns2012-01-01
Returns the first day of the year of DateTime
.
Syntax: YEAREND(DateTime)
Examples:
-
YEAREND('2010-12-31')
returns2010-12-31
-
YEAREND('2010-12-31 11:22:33')
returns2010-12-31
-
YEAREND('2012-01-01')
returns2012-12-31
Returns the last day of the year of DateTime
.
Syntax: DATEADD(Date, Interval, Units)
Examples:
-
DATEADD('2010-12-31', 1, 'days')
returns '2011-01-31' -
DATEADD('2010-12-31 11:22:33', 1, 'days')
returns '2011-01-31'
Equivalent to DateTimeAdd but returning result as a Date.
- Only the result is truncated to a Date value, input is passed straight through to
DateTimeAdd
function.
Syntax: BUSINESSDAYS(StartDate, EndDate)
Examples:
-
BUSINESSDAYS('2016-05-06','2016-05-13')
returns6
-
BUSINESSDAYS('2016-05-07','2016-05-13')
returns5
-
BUSINESSDAYS('2016-05-13','2016-05-13')
returns1
-
BUSINESSDAYS('2016-05-14','2016-05-14')
returns0
Number of weekdays (Monday - Friday) between two dates including both start and end.
- If
StartDate
is afterEndDate
, returnsNULL
.
Syntax: ISLEAPYEAR(Year)
Examples:
-
ISLEAPYEAR(2016)
returnsTRUE
-
ISLEAPYEAR(2010)
returnsFALSE
-
ISLEAPYEAR(1900)
returnsFALSE
-
ISLEAPYEAR(2000)
returnsTRUE
Is a year a leap year.
- Function takes a year not a date as an input, use
IsLearYear(Year([Date])
for a Date input.