Dynamics 365 Business Central: Dealing with Timezone on Webservice

In this topic, I’d like to discuss how we can manage the CurrentDateTime function in web services.

When working with web services, it’s essential to know that the CurrentDateTime function always returns the time in UTC. This can cause issues if the integration with third-party solutions operates in a different timezone.

Here’s an article from Microsoft Learn that confirms this.

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/webservices/managing-time-zones-with-web-services

How can we handle?

First, it’s important to understand how to convert UTC to the specific timezone you need.

There are many online tools available to assist with this conversion, such a UTC TIME.

Once you determine how many hours UTC is ahead of your specific timezone, add that duration to the CurrentDateTime function.

For this example, I’ll be using Singapore Time (SGT), which is 8 hours ahead.

Code

Let’s create a simple API page with two DateTime fields: UTC and SGT (Singapore Time).

page 50000 ApiPageName
{
    PageType = API;
    Caption = 'apiPageName';
    APIPublisher = 'publisherName';
    APIGroup = 'groupName';
    APIVersion = 'v1.0';
    EntityName = 'entityName';
    EntitySetName = 'entitySetName';
    SourceTable = "Company Information";
    DelayedInsert = true;

    layout
    {
        area(Content)
        {
            field(UTC; UTC) { }
            field(SGT; SGT) { }
        }
    }

    trigger OnAfterGetRecord()
    begin
        UTC := CurrentDateTime;
        SGT := CurrentDateTime + (((1000 * 60) * 60) * 8);
    end;

    var
        UTC, SGT : DateTime;
}

We will convert UTC to SGT by adding 8 hours.

1000 milliseconds * 60 seconds * 60 minutes equal to an hour.

Result

Let’s examine the end result from Postman and UTCtime.net.

  • Dynamics 365 Business Central: How to import Physical Inventory Order Recording
  • Dynamics 365 Business Central: Bank Revaluation, Realized or Unrealized?
  • Dynamics 365 Business Central: Dealing with Timezone on Webservice
  • Dynamics 365 Business Central: How to transfer custom fields to Posted/Ledger Entry