Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Datetime format in javascript gives me UTC time of datetime field

Posted on by 812

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

  • Verified answer
    Inogic Profile Picture
    Inogic 24,031 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,750 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

Quick Links

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,532 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,501 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans