Hi, Readers.
Regarding date calculation in Business Central, we have discussed the following two topics.
1. How to get Start Date and End Date (Period) of the week/month/quarter/year (Includes last and next) from a given date

2. About complex Due Date Calculations: More examples:
| Payment Method | Due Date Calculation | Document Date | Due Date |
| Cut-off date: 5th every month The payment: End of the month | D6+CM | 8/5/2024 8/6/2024 | 8/31/2024 9/30/2024 |
| Cut-off date: 5th every month The payment: 5th of the following month | D6+CM+5D | 8/5/2024 8/6/2024 | 9/5/2024 10/5/2024 |
| Cut-off date: 5th every month The payment: 10th of the following month | D6+CM+10D | 8/5/2024 8/6/2024 | 9/10/2024 10/10/2024 |
| Cut-off date: 5th every month The payment: 15th of the following month | D6+CM+15D | 8/5/2024 8/6/2024 | 9/15/2024 10/15/2024 |
| Cut-off date: 5th every month The payment: 20th of the following month | D6+CM+20D | 8/5/2024 8/6/2024 | 9/20/2024 10/20/2024 |
| Cut-off date: 5th every month The payment: 25th of the following month | D6+CM+25D | 8/5/2024 8/6/2024 | 9/25/2024 10/25/2024 |
| Cut-off date: 5th every month The payment: End of the following month | D6+1M+CM | 8/5/2024 8/6/2024 | 9/30/2024 10/31/2024 |
| Cut-off date: 5th every month The payment: 5th of the month after next | D6+1M+CM+5D | 8/5/2024 8/6/2024 | 10/5/2024 11/5/2024 |
| Cut-off date: 5th every month The payment: 10th of the month after next | D6+1M+CM+10D | 8/5/2024 8/6/2024 | 10/10/2024 11/10/2024 |
| Cut-off date: 5th every month The payment: 15th of the month after next | D6+1M+CM+15D | 8/5/2024 8/6/2024 | 10/15/2024 11/15/2024 |
| Cut-off date: 5th every month The payment: 20th of the month after next | D6+1M+CM+20D | 8/5/2024 8/6/2024 | 10/20/2024 11/20/2024 |
| Cut-off date: 5th every month The payment: 25th of the month after next | D6+1M+CM+25D | 8/5/2024 8/6/2024 | 10/25/2024 11/25/2024 |
| Cut-off date: 5th every month The payment: End of the month after next | D6+2M+CM | 8/5/2024 8/6/2024 | 10/31/2024 11/30/2024 |
| Cut-off date: 10th every month The payment: End of the month | D11+CM | 8/10/2024 8/11/2024 | 8/31/2024 9/30/2024 |
| Cut-off date: 15th every month The payment: End of the month | D16+CM | 8/15/2024 8/16/2024 | 8/31/2024 9/30/2024 |
| Cut-off date: 20th every month The payment: End of the month | D21+CM | 8/20/2024 8/21/2024 | 8/31/2024 9/30/2024 |
| Cut-off date: 20th every month The payment: 20th of 3 months later | D21+2M+CM+20D | 8/20/2024 8/21/2024 | 11/20/2024 12/20/2024 |
| Cut-off date: 25th every month The payment: End of the month | D26+CM | 8/25/2024 8/26/2024 | 8/31/2024 9/30/2024 |
| Cut-off date: 25th every month The payment: 5th of the following month | D26+CM+5D | 8/25/2024 8/26/2024 | 9/5/2024 10/5/2024 |
| Cut-off date: End of every month The payment: End of the month | CM+CM | 8/31/2024 9/1/2024 | 8/31/2024 9/30/2024 |
| Cut-off date: End of every month The payment: End of the month after next | CM+2M+CM | 8/31/2024 9/1/2024 | 10/31/2024 11/30/2024 |
PS: As for how to use the date formula, you can refer to the instructions in MS Learn below.
Work with Calendar Dates and Times -> Use Date Formulas: The date formula can contain a maximum of 20 characters, both numbers and letters. You can use the following letters, which are abbreviations for calendar units.
| Letter | Meaning |
|---|---|
| C | Current |
| D | Day(s) |
| W | Week(s) |
| M | Month(s) |
| Q | Quarter(s) |
| Y | Year(s) |
You can construct a date formula in three ways.
The following example shows how to use C, for current, and a time unit.
| Expression | Meaning |
|---|---|
| CW | Current week |
| CM | Current month |
The following example shows how to use a number and a time unit. A number cannot be larger than 9999.
| Expression | Meaning |
|---|---|
| 10D | 10 days from today |
| 2W | 2 weeks from today |
The following example shows how to use a time unit and a number.
| Expression | Meaning |
|---|---|
| D10 | The next 10th day of a month |
| WD4 | The next 4th day of a week (Thursday) |
The following example shows how you can combine these three forms as needed.
| Expression | Meaning |
|---|---|
| CM+10D | Current month + 10 days |
The following example shows how you can use a minus sign to indicate a date in the past.
| Expression | Meaning |
|---|---|
| -1Y | 1 year ago from today |
This is also used in the Job Queue. But even experienced developers need to confirm or do a simple calculation to ensure that there is no problem with the date. This is obviously difficult for non-developers.

In this post, I would like to share a simple Date Formula testing tool that I hope you can use when calculating dates.
For example,


Give it a try!😁
Source code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)
page 50110 "Date Formula Test Tool"
{
PageType = Card;
ApplicationArea = All;
UsageCategory = Administration;
layout
{
area(Content)
{
group(Input)
{
field(InputDate; InputDate)
{
Caption = 'Input Date';
ApplicationArea = All;
trigger OnValidate()
begin
CalculateDateFormula();
end;
}
field(DateFormula; DateFormula)
{
Caption = 'Date Formula';
ApplicationArea = All;
trigger OnValidate()
begin
CalculateDateFormula();
end;
}
}
group(Result)
{
field(ResultDate; ResultDate)
{
Caption = 'Result Date';
ApplicationArea = All;
Editable = false;
}
}
}
}
var
InputDate: Date;
ResultDate: Date;
DateFormula: DateFormula;
local procedure CalculateDateFormula()
begin
if (InputDate <> 0D) and (Format(DateFormula) <> '') then begin
ResultDate := CalcDate(DateFormula, InputDate);
end;
end;
}END
Hope this will help.
Thanks for reading.
ZHU




コメント