Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Microsoft Dataverse
Unanswered

Convert UTC Date Time to selected TimeZone in model driven app.

(0) ShareShare
ReportReport
Posted on by 41

I am trying to convert a datetime field value which comes in UTC to the time zone that a user can select in the "Time Zone" format field in dataverse using JS web resource but unable to do so. Not able to find the correct approach.

  • oyepanky Profile Picture
    181 on at
    Convert UTC Date Time to selected TimeZone in model driven app.
    Time zone values in Dataverse
    The “Time Zone” field (usually a lookup or option set) typically stores a TimeZoneCode, which corresponds to Microsoft’s TimeZoneDefinition table.

    Datetime conversion logic
    JavaScript itself cannot convert UTC to a specific Microsoft time zone ID directly — but you can use the Xrm.WebApi to retrieve the time zone offset and apply it.

    Here’s a general approach using JS Web Resource: 

    1.Get the user’s selected Time Zone Code (integer) 

    Assume this is stored in a field like new_timezone.
    var timeZoneCode = formContext.getAttribute("new_timezone").getValue(); // TimeZoneCode (int)

    Get the UTC datetime -  var utcDate = formContext.getAttribute("new_utc_datetime").getValue(); 

    Call LocalTimeFromUtcTime function = Dataverse exposes a function that you can call using Xrm.WebApi.online.execute: 
      function convertUtcToLocal(utcDate, timeZoneCode) {
        var request = {
            UtcTime: utcDate,
            TimeZoneCode: timeZoneCode,
            getMetadata: function () {
                return {
                    boundParameter: null,
                    parameterTypes: {
                        UtcTime: {
                            typeName: "Edm.DateTimeOffset",
                            structuralProperty: 1
                        },
                        TimeZoneCode: {
                            typeName: "Edm.Int32",
                            structuralProperty: 1
                        }
                    },
                    operationType: 0,
                    operationName: "LocalTimeFromUtcTime"
                };
            }
        };

        Xrm.WebApi.online.execute(request).then(
            function (response) {
                if (response.ok) {
                    response.json().then(function (result) {
                        var localDateTime = new Date(result.LocalTime);
                        console.log("Converted time:", localDateTime);
                        // You can now use this localDateTime as needed
                    });
                }
            },
            function (error) {
                console.error("Error converting time:", error.message);
            }
        );
    }

     

      Combine it = 
    var utcDate = formContext.getAttribute("new_utc_datetime").getValue();
    var timeZoneCode = formContext.getAttribute("new_timezone").getValue();
    if (utcDate && timeZoneCode) {
        convertUtcToLocal(utcDate, timeZoneCode);
    }

     
     

     

     

    Notes:


    • The TimeZoneCode is not always obvious — it maps to the TimeZoneDefinition entity, which contains values like “Pacific Standard Time” etc.
    • You can also retrieve a list of time zones via TimeZoneDefinition if needed.
    • This will work in model-driven apps where the JS is hooked into a form event or ribbon button. 
  • Suggested answer
    kalluu91 Profile Picture
    32 on at
    Convert UTC Date Time to selected TimeZone in model driven app.

    Then, convert the UTC date to local date as following:
    ```
    var timezoneOffset = Xrm.Utility.getGlobalContext().userSettings.getTimeZoneOffsetMinutes();
    var utcDate = new Date("2025-04-24T00:00:00.000Z"); // The UTC date
    utcDate.setUTCTime(utcDate.getTime() - (timezoneOffset * 60 * 1000));
    var localDate = new Date(utcDate.getUTCFullYear(), utcDate.getUTCMonth(), utcDate.getUTCDay(), utcDate.getUTCHours(), utcDate.getUTCMinutes(), utcDate.getUTCSeconds());
    ```
     
    Regards,
    Kim Anh
  • steeevid Profile Picture
    319 Super User 2024 Season 1 on at
    Re: Convert UTC Date Time to selected TimeZone in model driven app.

    I have the same issue, if I choose the column to be time zone dependent then Dataverse will just display the time in a grid view completely wrong (off by a certain amout of hours as it trying to display UTC), so I have to use time zone independent.

     

    Then if I want a feature like "due in days", which is = 'Due Date' - UTCtoday(), in power fx it will simply say cannot calculate time zone dependent value with time zone independent value.

     

    This is a very common issues that's being troubling many users, so far I don't understand why Microsoft don't implement a fix instead they annouced that this is a "feature" update in the 2023 release wave 2 to support time zone calculation in power fx

  • Drew Poggemann Profile Picture
    9,278 Most Valuable Professional on at
    Re: Convert UTC Date Time to selected TimeZone in model driven app.

    Hi @RiyaKaul ,

     

    Do you have a copy of the code you are trying to utilize?   When you say "convert a datetime field value which comes in UTC" how it is coming in?  Through an import, through the UI?  Overall this makes a difference on what will happen within the application and dataverse. 

     

    Dataverse will always store the value in UTC so if you take the value you receive and store into a Time Zone Independent field it should store this as the time passed in.  Check out this video with the multiple options and behaviors around datatime fields in Canvas Apps / Model Apps and Dataverse.

    https://youtu.be/cFxmRHt8uzg 

     

     

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Apps - Microsoft Dataverse

#1
stampcoin Profile Picture

stampcoin 15

#2
ankit_singhal Profile Picture

ankit_singhal 11 Super User 2025 Season 1

#3
mmbr1606 Profile Picture

mmbr1606 9 Super User 2025 Season 1

Overall leaderboard

Featured topics