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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Convert UTC Date Time ...
Power Apps
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.

I have the same question (0)
  • Drew Poggemann Profile Picture
    9,287 Most Valuable Professional on at

    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 

     

     

  • steeevid Profile Picture
    323 Super User 2024 Season 1 on at

    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

  • Suggested answer
    kalluu91 Profile Picture
    39 on at

    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
  • oyepanky Profile Picture
    361 on at
    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. 

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard