You often try to calculate the differences between two dates using the available functions in dynamics ax for dates. However, for example I have two dates such as start date = 07/07/2007 and end date = 
01/11/2016 where date caculator would show difference like 9 Years 3 Months 25
 Day. What if the same detail calculation I have to perform in X++. This post will save your time since we might sometimes require this code during ax programming.
 static void AXDateCaculator(Args _args)  
 {  
     int monthsDiff, daysDiff, yearsDiff;  
     date emergingDateMth, emergingDateYear;  
     str dateCalculated;  
     //Dates can be changed here  
     date startdate = mkDate(7, 7, 2007);  
     date endDate  = mkDate(1, 11, 2016);  
     ///  
     yearsDiff                  = intvNo(endDate,startDate,IntvScale::Year);       
     emergingDateYear              = DateMthFwd(startDate, yearsDiff*12);                          
     monthsDiff                 = intvNo(endDate,emergingDateYear,IntvScale::YearMonth);  
     if (monthsDiff < 0)  
     {    
       yearsDiff                = yearsDiff - 1;  
       emergingDateYear            = DateMthFwd(startDate, yearsDiff*12);                          
       monthsDiff               = intvNo(endDate,emergingDateYear,IntvScale::YearMonth);  
     }  
     emergingDateMth               = DateMthFwd(emergingDateYear, monthsDiff);  
     daysDiff                  = intvNo(endDate,emergingDateMth,IntvScale::MonthDay);  
     if (daysDiff < 0)  
     {  
       monthsDiff               = monthsDiff - 1;  
       emergingDateMth            = DateMthFwd(emergingDateYear, monthsDiff);  
       daysDiff                = intvNo(endDate,emergingDateMth,IntvScale::MonthDay);  
     }   
     
     if(daysDiff < 0)
     {
        daysDiff = 365 + daysDiff;
     }
     if (yearsDiff)  
     {  
       dateCalculated             += strfmt(" Years: %1", yearsDiff);              
     }   
     if (monthsDiff)  
     {  
       dateCalculated             += strfmt(" Months: %1", monthsDiff);             
     }  
     if (daysDiff)  
     {  
      dateCalculated             += strfmt(" Days: %1", daysDiff);              
     }  
     info(dateCalculated);   
 }  
After you execute this code you will see this result:Happy Daxing :)!
Hi, Thank you for the code piece you have shared. It is really helpful. But there is a bug in this code. For a date range like 21.12.2017 to 08.01.2018 will give -ve days value. To fix this please add the below code piece in line no 27. It will fix the issue.
ReplyDeleteif(daysDiff < 0)
daysDiff = 365 + daysDiff;
Thanks for the feedback.
DeleteHi even with the latest changes there is a bug.
ReplyDeleteadding "365 + dayDiff" does not consider the Leap year (since leap year contains 366 days)
If you try:
NoLeapYear:StartDate: 31/12/2021 to 01/01/2022 => 1 year and 1 days
LeapYear: StartDate: 31/12/2019 to 01/01/2021 => 1 year (bug)
The initial problem was
Line25: daysDiff = intvNo(endDate,emergingDateMth,IntvScale::MonthDay);
for some reason intvNo is not behaving as expected for days:
Fix:
Replace Line25 with:
daysDiff = _endDate - emergingDateMth;
and delete
if(daysDiff < 0)
{
daysDiff = 365 + daysDiff;
}