Number of days between two Date fields in D365FO (X++)

There are several scenarios where one would want to know the date difference between two dates.

  • Getting an investment tenor where user inputs investment startDate and maturidtyDate.
  • Getting the number of leave days when the employee picks a startDate and an endDate.
  • Number of days spent by an employee on probation.

 

We can do a simple subtraction : this.endDate – this.startDate.

We can also make use of the intvNo built in function. You can get the day, month and year difference between two dates.

    public static void main(Args _args)
    {
        date fromDate = str2Date("02/18/2018", 213);
        date toDate = str2Date("08/20/2022", 213);
     
        // Days
        info(strFmt("Day(s): %1", intvNo(toDate, fromDate, intvScale::YearMonthDay)));
        // Months
        info(strFmt("Month(s): %1", intvNo(toDate, fromDate, intvScale::YearMonth)));
        // Years
        info(strFmt("Year(s): %1", intvNo(toDate, fromDate, intvScale::Year)));
    }

Number of days between two Date fields in D365FO (X++)

Comments