Playing around with Date, utcDateTime and DateTimeUtil in AX2012

In this short post I would like to show you, how to play around with dates in AX2012, for the beginning let’s just manipulate a given utcDateTime, later we will see how to convert dates to utcDateTime.
To do so, the class DateTimeUtil  is a  good point to look at.

Add a year, month, day, hour, minute or second to a given utcDateTime

Let’s start with adding a year to today’s date.

static void DateTimeAddYear(Args _args)
{
utcDateTime todayPlusOneYear,
// Get the actual UTCDateTime based on the current system
todaysDateTime = DateTimeUtil::utcNow();
// Convert it to a string, just to show in on the info log
info(DateTimeUtil::toStr(todaysDateTime));
// Now add a year
todayPlusOneYear = DateTimeUtil::addYears(todaysDateTime, 1);
// And Info it out again
info(DateTimeUtil::toStr(todayPlusOneYear));
}
view raw DateTimeAddYear hosted with ❤ by GitHub

ax2012 Add a Year to utcDateTime

The other operations are very similar:

// Months:
addMonths(int _months)
// Days:
addDays(int _days)
// Hours:
addHours(int _hours)
// Minutes:
addMinutes(int _minutes)
// Seconds:
addSeconds(int _seconds)
view raw Add to DateTime hosted with ❤ by GitHub

Pass a negative integer to substract a given value from youre utcDateTime.

Convert between utcDateTime and date

static void DateTimeToDate(Args _args)
{
utcDateTime toDateTime;
// Get today's date and a null date
date fromDate = today(), toDate = dateNull();
// Info the date
info(date2str(fromDate,
321, /* style - ymd */
2, /* digits day - DateDay::Digits2 */
DateSeparator::Dot, /* separator */
2, /* digits month - DateMonth::Digits2 */
DateSeparator::Dot, /* separator */
4 /* digits year - DateYear::Digits4 */));
// Convert to utcDateTime
toDateTime = DateTimeUtil::newDateTime(fromDate, 0 /* time */);
// Info it out
info(DateTimeUtil::toStr(toDateTime));
// Create a date again
toDate = DateTimeUtil::date(toDateTime);
// ... and info it again
info(date2str(toDate,
321, /* style - ymd */
2, /* digits day - DateDay::Digits2 */
DateSeparator::Dot, /* separator */
2, /* digits month - DateMonth::Digits2 */
DateSeparator::Dot, /* separator */
4 /* digits year - DateYear::Digits4 */));
}
view raw DateTimeToDate hosted with ❤ by GitHub

ax2012 Change date to utcDateTime and back to date

You may also like...