Date functions
In Business Central, much of the data is linked to dates. Sales orders, invoices, or credit memos have document dates, posting dates, and a date of entry. You can also find dates on the customer record.
When processing these dates in code, you can use some date functions to get specific information out of a date or to change a date to a specific format.
The frequently used date functions are:
Date2DMY and Date2DWY
CalcDate
The Today and Time date functions return the current date and time. The WorkDate function returns the work date that is set in the application.
Date2DMY function
The Date2DMY (or Date to Day, Month, Year) function helps you get specific parts out of a certain date.
Number := Date2DMY(Date, What);
The What parameter specifies what the function should return.
1 - Corresponds to Day (1-31)
2 - Corresponds to Month (1-12)
3 - Corresponds to Year
// TODAY IS 04/17/2020
Message('%1', Today()) ;
// Displays : 04/17/2020
MyDatePart := Date2DMY(Today(), 1) ;
Message('%1', MyDatePart) ;
// Displays : 17
MyDatePart := Date2DMY(Today(), 2) ;
Message('%1', MyDatePart) ;
// Displays : 4
Date2DWY function
The Date2DWY (or Date to Day, Week, Year) function helps you get specific parts out of a certain date.
Number := Date2DWY(Date, What);
The What parameter specifies what the function should return.
1 - Corresponds to Day of the week (1-7, Monday = 1)
2 - Corresponds to the Week number (1-53)
3 - Corresponds to Year
// TODAY IS 04/17/2020
Message('%1', Today()) ;
// Displays : 04/17/2020
MyDatePart := Date2DWY(Today(), 1) ;
Message('%1', MyDatePart) ;
// Displays : 5
MyDatePart := Date2DWY(Today(), 2) ;
Message('%1', MyDatePart) ;
// Displays : 16
CalcDate function
The CalcDate function helps you calculate new dates, starting from a certain date.
NewDate := CalcDate(DateExpression [, Date]);
In the DateExpression parameter, you can provide how many days (D), weeks (W), months (M), quarters (Q), or years (Y) that you want to add or subtract. If you don't provide a Date parameter, the current system date is used.
// TODAY IS 04/17/2020
Message('%1', Today()) ;
// Displays : 04/17/2020
Message('%1', CalcDate('1W', Today())) ;
// Displays : 04/24/2020