Dynamics 365 Business Central: How to quickly change the date format according to the language id in AL

Dynamics 365 Business Central

Hi, Readers.
Today I would like to share another mini tip about Business Central, how to quickly change the date format according to the language id in AL.
As you know, the default date format displayed in Business Central is related to the Region setting in My Settings.
Region: The Region setting determines how dates, times, numbers, and currencies are shown or formatted.
Let’s look at a few simple examples.

Region: English (United States)

Date Format: 3/31/2024

Region: French (France)

Date Format: 31/03/2024

Region: Japanese (Japan)

Date Format: 2024/03/31

Region: German (Switzerland)

Date Format: 31.03.2024

So what should you do if you want to display another Region’s date format? Of course you can use the powerful System.Format(Any, Integer, Text) Method, but this time we use a simpler way, FormatDate method in codeunit 10 “Type Helper” (This method contains a LanguageId parameter)

PS: Dynamics 365 Business Central: List of Language IDs (1041,1033…) and Locale IDs (ja-JP,en-US…)

Let me do a simple test.
Region: English (United States)

Add a variable to display the new date format. Japanese (Japan) = 1041

Great.

Test code:

pageextension 50125 CustomerLedgerEntriesExt extends "Customer Ledger Entries"
{
    layout
    {
        addafter("Document Date")
        {
            field(NewDocumentDate; TypeHelper.FormatDate(Rec."Document Date", 1041))
            {
                ApplicationArea = All;
                Caption = 'New Document Date';
                Editable = false;
            }
        }
    }
    var
        NewDocumentDate: Text;
        TypeHelper: Codeunit "Type Helper";
}

Let’s look at an example. The user enters a date, selects a language, and the date is automatically converted to the regional format.

Test video: Youtube

Business Central short video: Changing the date format according to the language id in AL

Very simple, give it a try!!!😁

Test code: Github

tableextension 50112 CustomerExt extends Customer
{
    fields
    {
        field(50100; LanguageId; Integer)
        {
            Caption = 'Language Id';
            TableRelation = Language."Windows Language ID";
            ValidateTableRelation = false;
        }
        field(50101; Date; Date)
        {
            Caption = 'Test Date';
        }
        field(50102; FormattedDate; Text[20])
        {
            Caption = 'Formatted Date';
        }
    }
}

pageextension 50112 CustomerCardExt extends "Customer Card"
{
    layout
    {
        addafter(Name)
        {
            field(Date; Rec.Date)
            {
                ApplicationArea = All;
                Caption = 'Test Date';

                trigger OnValidate()
                begin
                    Rec.LanguageId := 0;
                    Rec.FormattedDate := '';
                end;
            }
            field(LanguageId; Rec.LanguageId)
            {
                ApplicationArea = All;
                Caption = 'Language Id';

                trigger OnValidate()
                var
                    TypeHelper: Codeunit "Type Helper";
                begin
                    if Rec.LanguageId = 0 then
                        Rec.FormattedDate := ''
                    else
                        Rec.FormattedDate := TypeHelper.FormatDate(Rec.Date, Rec.LanguageId);
                end;
            }
            field(FormattedDate; Rec.FormattedDate)
            {
                ApplicationArea = All;
                Caption = 'Formatted Date';
                Editable = false;
            }
        }
    }
}

PS: If you need to change the date format in the Report layout, you can refer to the following information.
Business Central 2023 wave 1 (BC22): Define regional settings per report using a region property

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL