Code highlighting

Monday, May 05, 2014

Q&A session: Working with UtcDateTime from X++

There were a couple questions asked in the post I wrote a while back about the use of UtcDateTime in AX.

Question 1:
Hi. how can put date in a field utcdatetime?
Answer:

The method newDateTime from class DateTimeUtil can be used to accomplish this, as it takes the date and time parts in separately as arguments, followed by the TimeZone enumeration in case a timezone offset needs to be factored in. Otherwise the UTC timezone will be used.

static void CreateUtcDateTimeFromDate(Args _args)
{
    date locDate = mkDate(5, 3, 2014);

    info(strFmt("Date converted to DateTime, with 0 for time and no timezone offset = %1",
        DateTimeUtil::newDateTime(locDate, 0)));
}

The result of executing this job would look like below:

Date converted to DateTime, with 0 for time and no timezone offset = 3/5/2014 12:00:00 am


Question 2:
Hi...Can you please tell the difference between UtcNow() and getSystemDateTime(). Both are returning GMT time only. But where as in msdn they described differently...
Answer:

The difference is you can "fix" the systemDateTime either through the API (DateTimeUtil::setSystemDateTime()) or through the Tools in the Dynamics menu, as shown on the screenshot:

Dynamics AX session date and time
Session date and time dialog

static void DifferenceBetweenUtcNowAndSystem(Args _args)
{
    info(strFmt("getSystemDateTime = %1", DateTimeUtil::getSystemDateTime()));
    info(strFmt("utcNow = %1", DateTimeUtil::utcNow()));
}

So, after having fixed the system datetime, the output from executing this job would be like below:

Info Message (11:01:28 pm) getSystemDateTime = 5/1/2014 12:00:00 am
Info Message (11:01:28 pm) utcNow = 5/5/2014 09:01:28 pm

If the system date time is not fixed, it will return the local system datetime, pretty much like utcnow.

Note, that msdn actually also describes a slight difference in how utcnow works - it seems it always runs on the server, so always returns the AOS datetime. I can't test it right now because only have a dev.setup with everything on 1 box. Once I get a 3box one, I will update this post with a confirmation.

No comments:

Post a Comment

Please don't forget to leave your contact details if you expect a reply from me. Thank you