RSS

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

10 Nov
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:

Image

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

Advertisements
 
Leave a comment

Posted by on November 10, 2013 in Development

 

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: