Working with Dates in X++

I was working on some date related functionality the other day during which I discovered some really useful date functions in X++. Some of you might be familiar with some of this already, however it is always good to have a blog post out there that lists them all for future reference. I have also added sample code for the usage of each function.

Initialization

A date variable can be initialized with a date constant. The date literal in X++ has a predefined format i.e. dd\MM\yyyy. Note that backslash is used instead of forward slash and unlike string constants neither single nor double quotes is used.

transDate = 01\01\2015;

today

Returns the date of the machine on which the code is run.

transDate = today();

systemDateGet

Returns the session date in AX. You can view/set the session date and time using File > Tools > Session date and time.

transDate = systemDateGet();

mkDate

Creates a date using the specified day, month and year components.

transDate = mkDate(31, 12, 2015);

Boundaries

Minimum value is January 1, 1900 also know as a null date. Maximum value is December 31, 2154.

info(strFmt("%1", dateNull()));
info(strFmt("%1", dateMax()));

Arithmetic

Plus and minus operators can be used to add or subtract days to a date. Minus operator used with date operands calculates the difference in days.

resultDate = transDate + 14;
info(strFmt("%1", resultDate));
resultDate = transDate - 21;
info(strFmt("%1", resultDate));
info(int2str(transDate - dateNull()));

Day related functions

dayName

Returns name of the specified day in a week. It takes a serial number representing day of week e.g. 1 (Monday), 7 (Sunday) and 8 (Monday).

info(dayName(2));

dayOfMth

Returns day component of the specified date.

info(int2str(dayOfMth(transDate)));

dayOfWk

Returns a serial number representing day of the week in which the specified date falls. You may use the output of this as an input to the dayName function.

info(int2str(dayOfWk(transDate)));

dayOfYr

Returns a serial number representing day of the year in which the specified date falls. e.g. 3 (Jan 3) and 32 (Feb 1).

info(int2str(dayOfYr(transDate)));

Week related functions

wkOfYr

Returns a serial number representing week of the year in which the specified date falls.

info(int2str(wkOfYr(transDate)));

dateStartWk

Returns a date of first day in the week in which the specified date falls.

resultDate = dateStartWk(transDate);
info(strFmt("%1", resultDate));

dateEndWk

Returns a date of last day in the week in which the specified date falls.

resultDate = dateEndWk(transDate);
info(strFmt("%1", resultDate));

Month related functions

mthName

Returns name of the specified month in a year. It takes a serial number representing month of year e.g. 1 (Jan), 9 (Sep) and 15 (Mar).

info(mthName(5));

mthOfYr

Returns month component of the specified date. You may use the output of this as an input to the mthName function.

info(int2str(mthOfYr(transDate)));

prevMth

Returns a date in the previous month that corresponds most closely to the specified date.

resultDate = prevMth(transDate);
info(strFmt("%1", resultDate));

nextMth

Returns a date in the following month that corresponds most closely to the specified date.

resultDate = nextMth(transDate);
info(strFmt("%1", resultDate));

dateStartMth

Returns date of the first day of the month in which the specified date falls.

resultDate = dateStartMth(transDate);
info(strFmt("%1", resultDate));

dateEndMth

Returns date of the last day of the month in which the specified date falls.

resultDate = dateEndMth(transDate);
info(strFmt("%1", resultDate));

dateMthFwd

Returns a date after adding the specified number of months to the date. You may also pass a negative value to subtract the months.

resultDate = dateMthFwd(transDate, 6);
info(strFmt("%1", resultDate));

Quarter related functions

date2Qtr

Returns a serial number representing quarter in which the specified date falls e.g. 1 (Jan-Mar).

dateString = int2str(date2Qtr(transDate));
info(dateString);

prevQtr

Returns a date in the previous quarter that corresponds most closely to the specified date.

resultDate = prevQtr(transDate);
info(strFmt("%1", resultDate));

nextQtr

Returns a date in the following quarter that corresponds most closely to the specified date.

resultDate = nextQtr(transDate);
info(strFmt("%1", resultDate));

dateStartQtr

Returns date of the first day of the quarter in which the specified date falls.

resultDate = dateStartQtr(transDate);
info(strFmt("%1", resultDate));

dateEndQtr

Returns date of the last day of the quarter in which the specified date falls.

resultDate = dateEndQtr(transDate);
info(strFmt("%1", resultDate));

Year related functions

year

Returns the year component of the specified date.

info(int2str(year(transDate)));

yearDiff

Calculates the difference in years between the two dates. First date should be greater than the second for a +ve difference.

info(int2str(yearDiff(transDate, dateNull())));

prevYr

Returns a date in the previous year that corresponds most closely to the specified date.

resultDate = prevYr(transDate);
info(strFmt("%1", resultDate));

nextYr

Returns a date in the following year that corresponds most closely to the specified date.

resultDate = nextYr(transDate);
info(strFmt("%1", resultDate));

dateStartYr

Returns date of the first day of the year in which the specified date falls.

resultDate = dateStartYr(transDate);
info(strFmt("%1", resultDate));

dateEndYr

Returns date of the last day of the year in which the specified date falls.

resultDate = dateEndYr(transDate);
info(strFmt("%1", resultDate));

Conversion functions

date2num

Converts a date to an integer that corresponds to the number of days since 1 January, 1900.

dateString = int2str(date2num(transDate));
info(dateString);

date2str

Converts a date to a string using the provided format specifiers.

dateString = date2str(transDate, 321, DateDay::Digits2, DateSeparator::Slash, DateMonth::Digits2, DateSeparator::Slash, DateYear::Digits4, DateFlags::None);
info(dateString);

Pass -1 for all format specifiers to get a date formatted as per the user’s regional settings.

dateString = date2str(transDate, -1, -1, -1, -1, -1, -1, -1);
info(dateString);

date2StrUsr

Converts a date in to a string as per the user’s regional settings.

dateString = date2StrUsr(transDate);
info(dateString);

date2StrXpp

Converts a date in to an X++ date literal. Use this when adding date ranges to query build data sources using code.

dateString = date2StrXpp(transDate);
info(dateString);

date2Julian

Converts a Gregorian date into Julian date with format yyyyddd. ddd is the number of days elapsed in the year yyyy.

dateString = date2Julian(transDate);
info(dateString);

str2Date

Converts a string to date.

resultDate = str2Date("2015.12.25", 321);
info(strFmt("%1", resultDate));

str2DateDMY

Converts a string in DMY format to a date.

resultDate = str2DateDMY("14-08-1947");
info(strFmt("%1", resultDate));

Leave a comment