Tip #66 | UTC Calculation

I have not blogged as much as I want to recently. Not because there is nothing to write about, but because I am so insanely busy.

However, today I ran into a question that required first Google and then Statical Prism to solve.

UTC Calculation in Dynamics NAV (Or Business Central)

This used to be cumbersome and required dirty tricks. Alternatively (and this is how I managed to solve it until today) you can setup a UTC service tier.

But, with the help of Prism I found that Codeunit 358 now has this code

[External] ConvertToUtcDateTime(LocalDateTime : DateTime) : DateTime
IF LocalDateTime = CREATEDATETIME(0D,0T) THEN
  EXIT(CREATEDATETIME(0D,0T));
DotNetDateTimeOffset := DotNetDateTimeOffset.DateTimeOffset(LocalDateTime);
DotNetDateTimeOffsetNow := DotNetDateTimeOffset.Now;
EXIT(DotNetDateTimeOffset.LocalDateTime - DotNetDateTimeOffsetNow.Offset);

So no more stupid workarounds, just use this instead.

Please hold on for more stuff on my blog. I recently converted one of my customers to SQL Azure with Windows Virtual Desktop, Azure Logic Apps, Blob storage & Azure Functions. So much to blog about. #ServerlessComputing

I’m also starting on a new contract next week that will help me multiply my skills in an efficient way. Very enthousiastic to try that out for the first time.

So hang on and keep coding….

2 thoughts on “Tip #66 | UTC Calculation

  1. Reisinger Christian

    I would not recommend using the standard here. it also works without .net, just take advantage of the DateFormat 9 (which always is calculated as UTC)

    procedure GetUTCTimeOffset(D: Date): Integer
    var
    T: Time;
    Hours: Integer;
    begin
    T := 120000T;
    Evaluate(Hours, CopyStr(Format(CreateDateTime(D, T), 0, 9), 12, 2));
    exit(12 – Hours);
    end;

    Like

    Reply

Leave a Reply to Mark Brummel Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.