Showing posts with label DateTimeUtil. Show all posts
Showing posts with label DateTimeUtil. Show all posts

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.

Thursday, March 17, 2011

How to split a time in hours, minutes and seconds

Just like we can split a date into the year, month and day part, we can do something similar for a time.  We can split the time into hours, minutes and seconds.  And the nice part: we can use the same tool for this as we use for splitting a date.  We can use the DateTimeUtil class.

Here's a short example:

static void GetMyTimeSplit(Args _args)
{   
    TransDateTime   myDateTime=DateTimeUtil::applyTimeZoneOffset(DateTimeUtil::getSystemDateTime(),Timezone::GMTPLUS0100BRUSSELS_COPENHAGEN_MADRID);
    int             hours;
    int             minutes;
    int             seconds;
    ;
    info(datetime2str(myDateTime));
    
    hours=DateTimeUtil::hour(myDateTime);
    minutes=DateTimeUtil::minute(myDateTime);
    seconds=DateTimeUtil::second(myDateTime);
    info(strfmt('Hours %1 - Minutes %2 - Seconds %3',int2str(hours),int2str(minutes),int2str(seconds)));
}

The DateTimeUtil class is like a kind of Swiss army knife when it comes to date and time handling.
As you see from the example above, we use it to populate our TransDateTime variable with the current date and time also.