Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

Datetime format in javascript gives me UTC time of datetime field

(0) ShareShare
ReportReport
Posted on by 814

Hello,

I need to read Date value of one DateTime field in my Dynamics CRM Form using JavaScript.

I am able to read the value but it is not exactly same what I can see in CRM.Date t

In CRM Form, selected value is: 11 Nov 2020 8:00am

JavaScript Code return value as 11 Nov 2020 different time, as per UTC.

How do i make JS read write value

  • Suggested answer
    Inogic Profile Picture
    Inogic 24,102 on at
    RE: Datetime format in javascript gives me UTC time of datetime field

    Hi Ravi,

    You can use getTimeZoneOffsetMinutes function from global context as below to get the correct date and time value. 

    var dateTimeFieldValue  = Xrm.Page.getAttribute("FieldName").getValue()

    var utcDate = new Date(dateTimeFieldValue.getUTCFullYear() , dateTimeFieldValue.getUTCMonth() , dateTimeFieldValue.getUTCDate(), dateTimeFieldValue.getUTCHours(), dateTimeFieldValue.getUTCMinutes(), dateTimeFieldValue.getUTCSeconds() ,dateTimeFieldValue.getUTCMilliseconds());

    var correctFieldDateTimeValue = new Date(utcDate.setMinutes(utcDate.getMinutes() + Xrm.Utility.getGlobalContext().userSettings.getTimeZoneOffsetMinutes()));

    Hope this helps!

  • Suggested answer
    Fubar Profile Picture
    Fubar 2,752 on at
    RE: Datetime format in javascript gives me UTC time of datetime field

    This is a general JavaScript date-time issue when dealing with date objects and UTC and browser local time.  

    Depending on exactly what you are wanting to do the answer may be slightly different, and the methods may be different, but on a Date object  .toLocaleDateString()  or .toLocaleDateString("<country code>") if you want it in a specific country timezone  etc (also a similar method exists exists for the time component). 

    developer.mozilla.org/.../toLocaleDateString

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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans