Dynamics 365 Business Central: A simple Date Formula testing tool

Dynamics 365 Business Central

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 MethodDue Date CalculationDocument DateDue Date
Cut-off date: 5th every month
The payment: End of the month
D6+CM8/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+5D8/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+10D8/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+15D8/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+20D8/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+25D8/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+CM8/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+5D8/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+10D8/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+15D8/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+20D8/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+25D8/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+CM8/5/2024
8/6/2024
10/31/2024
11/30/2024
Cut-off date: 10th every month
The payment: End of the month
D11+CM8/10/2024
8/11/2024
8/31/2024
9/30/2024
Cut-off date: 15th every month
The payment: End of the month
D16+CM8/15/2024
8/16/2024
8/31/2024
9/30/2024
Cut-off date: 20th every month
The payment: End of the month
D21+CM8/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+20D8/20/2024
8/21/2024
11/20/2024
12/20/2024
Cut-off date: 25th every month
The payment: End of the month
D26+CM8/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+5D8/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+CM8/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+CM8/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.

LetterMeaning
CCurrent
DDay(s)
WWeek(s)
MMonth(s)
QQuarter(s)
YYear(s)

You can construct a date formula in three ways.
The following example shows how to use C, for current, and a time unit.

ExpressionMeaning
CWCurrent week
CMCurrent month

The following example shows how to use a number and a time unit. A number cannot be larger than 9999.

ExpressionMeaning
10D10 days from today
2W2 weeks from today

The following example shows how to use a time unit and a number.

ExpressionMeaning
D10The next 10th day of a month
WD4The next 4th day of a week (Thursday)

The following example shows how you can combine these three forms as needed.

ExpressionMeaning
CM+10DCurrent month + 10 days

The following example shows how you can use a minus sign to indicate a date in the past.

ExpressionMeaning
-1Y1 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

コメント

Copied title and URL