Dynamics 365 Business Central: How to quickly get Coordinated Universal Time (UTC Time)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to quickly get Coordinated Universal Time (UTC Time) in Business Central.

First of all, in Business Central we can use System.CurrentDateTime() Method to get the current DateTime.
For example:

My Settings:
Region: English (United States)
Time zone: (UTC+09:00) Osaka, Sapporo, Tokyo

08/25/21 10:12 AM

Note: The output Datetime format is related to your Region setting.
Region: Japanese (Japan)
Time zone: (UTC+09:00) Osaka, Sapporo, Tokyo

21/08/25 10:20

Okay, let’s return to the topic, how to get Coordinated Universal Time (UTC Time)?

This time we can easily use System.Format(Any, Integer, String) Method.
With the Format function in Business Central, you can set the format of the source expression for various data types in AL.

Standard DateTime Formats: The following table shows the standard DateTime formats with the regional setting of a European country/region.

Europe DateTimeFormatExample
<XML format> Warning: A DateTime is stored in the database as Coordinated Universal Time (UTC). For example, on a computer in the (UTC+01:00) Brussels, Copenhagen, Madrid, Paris time zone, UTC is one hour earlier than local time on the computer and probably two hours earlier during summer time.92021-04-05T03:35:55.553Z

Let’s see some more details.

Test Video:

Source Code:

pageextension 50100 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        LocalDateTime: DateTime;
        UTC: Text;
        Msg: Label 'Local Datetime is %1.\UTC is %2.';
    begin
        LocalDateTime := CurrentDateTime;
        UTC := Format(CurrentDateTime, 0, 9);
        Message(Msg, LocalDateTime, UTC);
    end;
}

Update: Hint from Natalie “BC Docs Librarian” Karolak.

You can use codeunit 10 “Type Helper” as well.

For example: TypeHelper.GetCurrUTCDateTimeAsText()

Source Code:

pageextension 50100 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        LocalDateTime: DateTime;
        UTC: Text;
        Msg: Label 'Local Datetime is %1.\UTC is %2.';
        TypeHelper: Codeunit "Type Helper";
    begin
        LocalDateTime := CurrentDateTime;
        UTC := TypeHelper.GetCurrUTCDateTimeAsText();
        Message(Msg, LocalDateTime, UTC);
    end;
}

PS: There are other useful functions in this codeunit, give it a try!

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました