Dynamics 365 Business Central: How to easily convert DateTime between UTC and local time (UTC Time Zone Converter)

Dynamics 365 Business Central

Hi, Readers.
Today I would like to briefly talk about how can we easily convert Date Time between UTC and local time (UTC Time Zone Converter).
Coordinated Universal Time (UTC) is the primary time standard globally used to regulate clocks and time. And in Business Central, you can define the time zone where you’re located.
When you first sign into Business Central, the time zone is set based on your company’s address. Change it if it doesn’t fit your physical location.

Time-related data is a common requirement for many applications. In Dynamics 365 Business Central: How to quickly get Coordinated Universal Time (UTC Time), We have briefly discussed how to quickly get Coordinated Universal Time (UTC Time).

But if we need to convert, is there any easy way? Yes, we can use timezone offset. A timezone offset is the difference in hours between a particular time zone and UTC. For example, (UTC+09:00) Osaka, Sapporo, Tokyo: Japan time zone, timezone offset is +9 hours.

In Dynamics 365 Business Central: How to get timezone offset in AL, we briefly discussed how to get the timezone offset in AL, using GetUserTimezoneOffset method in codeunit 10 “Type Helper”.

Since we get the Duration Data Type, it’s easy to calculate. For example, we can use the current datetime to calculate UTC via timezon offset.

This time let me make a simple converter.

For example,

Test video:

Very simple. 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 50103 "UTC Time Zone Converter"
{
    PageType = Card;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(TimeZone)
            {
                field(TimeZoneOffset; TimeZoneOffset)
                {
                    Caption = 'TimeZone Offset';
                    ApplicationArea = All;
                    Editable = false;
                }
            }
            group(Convert)
            {
                field(LocalDateTime; LocalDateTime)
                {
                    Caption = 'Local DateTime';
                    ApplicationArea = All;

                    trigger OnValidate()
                    begin
                        if LocalDateTime = 0DT then
                            UTCDateTime := 0DT
                        else
                            UTCDateTime := LocalDateTime - TimeZoneOffset;
                    end;
                }
                field(UTCDateTime; UTCDateTime)
                {
                    Caption = 'UTC DateTime';
                    ApplicationArea = All;

                    trigger OnValidate()
                    begin
                        if UTCDateTime = 0DT then
                            LocalDateTime := 0DT
                        else
                            LocalDateTime := UTCDateTime + TimeZoneOffset;
                    end;
                }
            }
        }
    }
    var
        LocalDateTime: DateTime;
        TimeZoneOffset: Duration;
        UTCDateTime: DateTime;

    trigger OnOpenPage()
    var
        TypeHelper: Codeunit "Type Helper";
    begin
        if not TypeHelper.GetUserTimezoneOffset(TimezoneOffset) then
            TimezoneOffset := 0;
    end;
}

PS:
1. About dates in Business Central

2. Managing time zones in web services

3. Dynamics 365 Business Central: Date, Time, DateTime Conversions (Date Type -> DateTime Type, DateTime Type -> Date Type, DateTime Type -> Time Type)

4. Dynamics 365 Business Central: Date-Time Dialog

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL