Thursday, December 21, 2017

AX, X++ - How calculate difference with date types

Best for date difference calculation is intvNo() function. But exists another methods too.
static void zDate(Args _args)
{
  date d1 = mkDate( 15, 11, 2017 );
  date d2 = mkDate( 20, 12, 2017 );       
  int iDiffDays, iDiffMonths, iDiffYears;
  int64 iDiffSec;
  utcDateTime utcd1, utcd2;
    
  /* -- difference in days (date type) */
    
  iDiffDays = d2 - d1;
  info( "Days diff. = " + int2str( iDiffDays ) );
        
  /* -- difference in seconds (utcDateTime type */
    
  utcd1 = DateTimeUtil::newDateTime( d1, 0 );
  utcd2 = DateTimeUtil::newDateTime( d2, 0 );
  iDiffSec = DateTimeUtil::getDifference( utcd2, utcd1 );
  info( "Days diff. = " + int642str( iDiffSec ) );
   
  /* -- intvNo() */
    
  info( "" );
  iDiffDays = intvNo( d2, d1, intvScale::Day );
  info( "Days = " + int2str( iDiffDays ) );

  iDiffDays = intvNo( d2, d1, intvScale::MonthDay );
  info( "Days (total) = " + int2str( iDiffDays ) );
    
  iDiffMonths = intvNo( d2, d1, intvScale::Month );
  info( "Months = " + int2str( iDiffMonths ) );
    
  iDiffYears = intvNo( d2, d1, intvScale::Year );
  info( "Years = " + int2str( iDiffYears ) );   
}
Output:
Days diff. = 35
Days diff. = 3024000

Days = 5
Days (total) = 35
Months = 1
Years = 0
PS: Better way how make difference in days (3/2024):
iDiffDays = DateTimeUtil::date( sysDatabaseLog.createdDateTime ) - 
            DateTimeUtil::date( sysDatabaseLogInsert.createdDateTime );

No comments:

Post a Comment