Dynamics 365 Business Central: How to quickly check whether a year is a leap year in AL

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share another mini tip about Business Central, how to quickly check whether a year is a leap year.
Leap years are years where an extra day is added to the end of the shortest month, February. This so-called intercalary day, February 29, is commonly referred to as leap day. For example, 2024 is a leap year. The 2024 leap day will fall on February 29, 2024. The next one is February 29, 2028. So any year that is evenly divisible by 4 is a leap year: for example, 1988, 1992, and 1996 are leap years.
However, there is still a small error that must be accounted for. To eliminate this error, the Gregorian calendar stipulates that a year that is evenly divisible by 100 (for example, 1900) is a leap year only if it is also evenly divisible by 400.
For this reason, the following years are not leap years: 1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600. This is because they are evenly divisible by 100 but not by 400.
The following years are leap years: 1600, 2000, 2400. This is because they are evenly divisible by both 100 and 400.
If we only check from the year, we may need the following processing: More details: Method to determine whether a year is a leap year:

  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  4. The year is a leap year (it has 366 days).
  5. The year is not a leap year (it has 365 days).

So does it have to be the same in AL? No, there is actually an easier way. In codeunit 10 “Type Helper”, you can find a standard method, procedure IsLeapYear(Date: Date): Boolean.

Let’s see a simple example. Check whether the entered date is a leap year.

Test video:

Very simple, give it a try!!!😁

Source code: Github

pageextension 50121 CustomerCardExt extends "Customer Card"
{
    layout
    {
        addafter(Name)
        {
            field(CheckDate; CheckDate)
            {
                Caption = 'Check Date';
                ApplicationArea = All;

                trigger OnValidate()
                var
                    TypeHelper: Codeunit "Type Helper";
                begin
                    if TypeHelper.IsLeapYear(CheckDate) then
                        IsLeapYear := IsLeapYear::Yes
                    else
                        IsLeapYear := IsLeapYear::No;
                end;
            }
            field(IsLeapYear; IsLeapYear)
            {
                Caption = 'Is Leap Year';
                ApplicationArea = All;
                Editable = false;
            }
        }
    }

    var
        CheckDate: Date;
        IsLeapYear: Option ,Yes,No;
}

END

Hope this will help.

Thanks for your reading.

ZHU

コメント

Copied title and URL