web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Unanswered

Looking for a workaround: CreateDateTime does not account for Timezone in OnInstall or OnUpgrade code

(0) ShareShare
ReportReport
Posted on by

Hi,

I just reported this bug here https://github.com/microsoft/AL/issues/7160 But I'm a bit pessimistic that they might just dismiss it as they closed all the channels to report bugs in the system app directly (or hid them somewhere?!)

tl;dr: I'm trying to obsolete some Date and Time fields from our legacy solution in favor of using DateTime in the extension world. So naturally, I wrote an Upgrade-Codeunit to migrate the data to the new fields just to find out that CreateDateTime() and Time() both act like the session is in the UTC-TimeZone even when I explicitly set the timezone with the SessionSettings-Object to the Timezone the Data got entered in (W. Europe Standard Time)

I don't mind "setting off" the calculated times to get my desired outcome (user sees the time in the "Time"-field represented in the DateTime-Field again - no 2hour shift) But I'm a little lost on how to achieve that goal. - Because i somehow need to find the difference between the local time and UTC to use it for calculation... any Ideas?

I don't want to set the Target to "OnPrem". Otherwise i could use DotNET but or modules are on AppSource and they are really don't like OnPrem there.

My very last option would be to have a function run by the user.. or some webservice trickery. But I would prefer not to overcomplicate and use the tools that where designed for this.

regards

Stefan

I have the same question (0)
  • Inge M. Bruvik Profile Picture
    1,105 Moderator on at

    You should look in the Type helper codeunit. There are several functions there for handling offsets between local time and UTC time.

    I am sure you will find something there that you can use.

    Type helper is Codeunit 10 in the base application.

  • Community Member Profile Picture
    on at

    Thank you for your input. I've followed your suggestion and spent quite some time tinkering with CU10 and other stuff i found in there (DotNet_* codeunits - lots of them).

    I thought i was really close to the solution. But I can't find a Saas compatible function that would calculate the daylight savings properly for the given date. All the functions apply the daylight savings time based on the current date. There isn't even a function where i could pass the timezone and the date at the same time.

  • Inge M. Bruvik Profile Picture
    1,105 Moderator on at

    Isn't what you are trying to do just to get the difference between the UTC time and the users local time according to the time zone setting?

    So currentdatetime will give you the users local time and then you can compare that to the UTC time you get from the type helper codeunit?

    The datetime field in BC is always saved as UTC time. So to calculate the time passed between two datetime instances you do not really have to care about the time zone.

    Or am i misunderstanding something here?

  • Community Member Profile Picture
    on at

    Yeah, maybe i didn't make it clear enough. I need to patch data with dates all around the year. And the Type Helper only returns the offset for the current system date.

    example:

    Our customers are in the western europe time zone. (GMT +1h) but we also have daylight savings time during half of the year. Which means we currently have an offset of 2 hours to UTC. But in January, there is only 1 hour offset to UTC.

    So when i have to turn date "13.01.2022" and time "08:00" into UTC i should generate "13.01.2022 07:00" because the offset in January is only 1 hour. But date "13.07.2022" and time "08:00" should become "13.07.2022 06:00" because in summer, there is a 2 hour offset.

    Type helper only allows me to get the offset of "Now()" so it woudn't allow me to properly calculate the times correct for half the year.

    However: i wrote a solution with the help of DotNET for my current issue and supplied MS with the tested solution.  (see my last commetn on github) - I hope they add the needed functionality to the Type Helper soon so we all can use it in Saas-extensions as well.

  • Amit Baru Profile Picture
    3,037 on at

    hi,

    can you share your written code.

    Regards

    Amit Sharma

  • Community Member Profile Picture
    on at

    It's all shared in the github issue i linked in the initial post.

  • Nitin Verma Profile Picture
    21,698 Moderator on at

    Hi,

    You can try to use

    TypeHelper: Codeunit "Type Helper";

    TypeHelper.GetCurrUTCDateTime()

  • Community Member Profile Picture
    on at

    They don't calculate the daylights saving times correctly for half the year. - already tried.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,143

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,694 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,067 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans