Wednesday, January 4, 2012

Different ways of getting the current date: Which one should you pick?

There are a number of ways in Dynamics Ax to get the current date.
Maybe you've tried some of them, and most likely they all returned the same result.  Albeit that's actually depending on your installation and setup.

What's the difference?  Which method should you use when writing code to get the current date?
A short overview:

today

The today function uses the date of the machine.
This method is deprecated, for the simple reason that there's no support for different time zones.


systemDateGet

This function returns the session date.  If there is no specific session date set, it will return the system date.
Remember what the session date is?
It's a feature which allows you to change the date used within your specific Ax session, fe to back-date transactions.  Changing the posting date (all modules), changing the invoice date (and subsequent due date).  Note that for system fields of a table like CreatedDateTime and ModifiedDateTime, the computer date is used and not the Ax system date.

You can set the session date under Tools - Session:



DateTimeUtil::getSystemDateTime

This method gets you the session date as well, if not specified the system date.
The method returns a utcdatetime value.  You can use DateTimeUtil::Date to extract the date part.
Only DateTimeUtil::getSystemDateTime compensates for the time zone of the user.
Remember there can be a difference in date and time between server and client.
From application point of view, when establishing business logic, this method is the natural choice.

This last method is considered best practice in Ax.

In code, the different options:

static void WhatsTheDate(Args _args)
{  ;
   info(date2str(today(),123,2,2,2,2,4));
   info(date2str(systemdateget(),123,2,2,2,2,4));
   info(date2str(DateTimeUtil::date(DateTimeUtil::getSystemDateTime()),123,2,2,2,2,4));
   info(date2str(DateTimeUtil::date(DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(), DateTimeUtil::getUserPreferredTimeZone())),123,2,2,2,2,4));
}


Conclusion: Use DateTimeUtil::getSystemDateTime, unless you have very good reasons not to.

In a following blog post, I'll dive deeper into the functions available with DateTimeUtil, with different timezone support and user-related timezone settings.

4 comments:

  1. Hi. i was wondering if you guys can help me with a little problem to get the right dimension in AX2012.

    In AX 2009 when i wanted to get the third dimension i had to do this:

    qbd.addGroupByField(fieldid2ext(fieldnum(ledgertrans , Dimension),3 ));

    but i cannot figure it out in AX2012.

    ReplyDelete
  2. Hi, i had a problem as time zone.But i don't know how to slove this problem.If you have the experience , plz tell me . Thank you very much.
    And my quesetion is in here.
    https://community.dynamics.com/ax/f/33/t/121265.aspx

    ReplyDelete
  3. Thank you so much for showing such a big number of ways in Dynamics Ax to get the current date. I'm sure that it will be helpful for me and I'll use some ways in my projects.

    ReplyDelete

  4. we are the leading oracle fusion trainers in Hyderabad. we have our services online also. we have globally recognized the platform is http://www.erptree.com where people come and subscribe for various oracle related courses. we provide free instance access for our subscribers.
    for more details please visit our site erptree.com thank you.


    Oracle Fusion HCM Training

    ReplyDelete