Name is Anant Dubey and the intent to create this blog is to discuss the problems and issues that developer face in the dynamics AX development and to share the new things that come up with the new version of AX.

Friday, 19 December 2014

How to get DateNull in in ax 2012

One of my co new bie developer with the Dot net background had really hard time in figuring out the dateNull in x++ due to two reasons
  1. X++ does not have intellisense like Dot net which could help the newbie like my colleague
  2. The null value in  x++ debugger is shown as “Zero (0)”. So lots of new bie take the null value for date time as 0 and go in wrong direction
So i just thought to do a simple post on it. I know for a senior or the developer at mid level may find this simple and irrelevant to be posted at the blog, however i think this may help the new developer who are at the edge of starting new career in x++.
So here it goes
DateNull for the date can be checked by two ways
  1. Global::DateNull()
  2. or like this
    if (endDate != mkdate(1,1,1900)
The minimum value for the date in x++ is 1/1/1900 and max value is 12/31/2154
If you have utcDateTime here is how you can set dateNull and maximum value
todayDate = DateTimeUtil::minValue();
maximumDate =  DateTimeUtil::maxValue();
blankUtcDate = utcDateTimeNull();

Some Useful date functions in AX 2012

There are a lot of functions in dynamics Ax for dates. Followings are some date time functions I used extensively.
Some times we need to get month, year form date we can get these with the use of following functions in Dynamics ax
int _Months;
int _Years;
//Get day from date
_DayOfMonth =dayOfMth(systemdateget());;
// Get month from date
_Months = dayOfMth(systemdateget());
//Get month from date
//Get year from date
_Years =year(systemdateget());
This functions help you create date from input values.  For example if you have input for month and year, and want to create date for first of selected month of selected year, you can create it as.

Date _CustomDate;
Date _LastDateOf Month;
Int month_number;
Int years_numbers;
Int day_number;
Month_number=4 ; // say April
_customDate =mkdate(Day_number, Month_number, Years_Number);

Info _customDate;

This method returns the last date of month what ever the date we given to it. for example if we give the above created  date then it will return the last date of april
_LastDateOfMonth = endmth(_customDate);

Usually we need loop through next month, next year , nextQtr,prevQtr, pervious month, pervious year.
 We are use them as

Date _nextDate;

_NextDate = nextMth(today);
_NextDate =preYr(today);
 Usually we have to convert date to UTCDateTime. That is usally case when we have to query on createdatetime filed of very table, which filled when new row is created.
We for this purpose we have to create use DateTimeUtil::newDateTime function. This function takes two values one for date, and second for time. So time values will be range beween 0 and 86400. It means when value time is 00:00:00 value will be 0 and when time is 23:59:59 then value will be 86400. Consider following code which creates start and end date for same date.

Date _Currentdate;
utcDateTime _UtcStartPeriod;
utcDateTime _UtcEndPeriod;

_UtcCurrentPeriod =  DateTimeUtil::newDateTime(_currentdate,0);
_UtcEndPeriod = DateTimeUtil::newDateTime(_UtcEndPeriod,86400);