How to compare two dates in MS Dynamics AX 2009?

pjuzeliunas picture pjuzeliunas · Aug 11, 2011 · Viewed 8.5k times · Source

How to compare two dates (instances of type Date, not utcDateTime) in MS Dynamics AX 2009?

I want to check if particular date, taken from the table, is before (or after) the another one. Both are date types.

Is there a way to convert date to utcDateTime datatype?

Answer

Jan B. Kjeldsen picture Jan B. Kjeldsen · Aug 12, 2011

How to compare two dates?

Use the comparison operators < <= >= > == and !=.

if (LedgerTrans.TransDate > systemDateGet() - 3)
    LedgerTrans.TransDate = systemDateGet() - 3; 

This works in selects too:

select firstonly LedgerTrans 
    where LedgerTrans.TransDate > systemDateGet() - 3;

It works in query input ranges as well: >13-08 will select select dates after August 13'th of the current year. See also: http://kashperuk.blogspot.com/2010/02/utcdatetime-in-dynamics-ax-2009.html

How to convert a date to utcDateTime?

DateTimeUtil::newDateTime(systemDateGet(), 0, DateTimeUtil::getUserPreferredTimeZone()));

See also: http://msdn.microsoft.com/en-us/library/cc584924.aspx

There is no need to convert Date to utcDateTime to compare two dates.