Raas
Raas

Reputation: 261

Converting Date to CurrentCompany timeZone in Dynamics ax x++

I have a scenario where i need to convert a Datefield(joindate) to currentcompany timezone date. And then i need to compare this with anotherdate(startdate). If the difference is more than 365 days i need to give an warning. Can someone help me in this. Thanks in advance.

Upvotes: 0

Views: 5580

Answers (2)

10p
10p

Reputation: 6733

If Datefield(joindate) is of type date and not utcDateTime then DateTimeUtil::newDateTime() should be used to convert it to utcDateTime:

utcDateTime joinDateTime = DateTimeUtil::newDateTime(joindate, 0, DateTimeUtil::getCompanyTimeZone());

DateTimeUtil::getDifference() can be used to get the number of seconds between the utcDateTime values.

If both Datefield(joindate) and anotherdate(startdate) are of type date and not utcDateType then no conversion is required at all, and you can check whether the difference is more than 365 as follows:

if (joindate - startdate > 365) {}

If the above assumptions are wrong, see DAXaholic's answer.

Upvotes: 1

DAXaholic
DAXaholic

Reputation: 35368

You can apply a timezone to an utcdatetime via DateTimeUtil::applyTimeZoneOffset
The company timezone can be retrieved by calling DateTimeUtil::getCompanyTimeZone

Afterwards calculate the difference by calling DateTimeUtil::getDifference, which returns the difference in seconds so you have to compare that with the seconds per year.
To avoid inserting a 'magic' number, use the constants in the macro library TimeConstants.

Upvotes: 2

Related Questions