Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Transform DateTime field to Date javascript

Posted on by 115

Im using this code to retrieve an entity with a datetime field. When i retrieve the field i get it in this format  "Date(16456222000000)" as string, how can i transform that to a date that i can compare?


function retrieveRecord(recordId) { var clientURL = Xrm.Page.context.getClientUrl(); var req = new XMLHttpRequest(); var query = "/api/data/v8.2/entitySetName?$filter= idlogicalname eq " + recordId; req.open("GET", encodeURI(clientURL + query), true); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json;charset=utf-8"); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.onreadystatechange = function () { if (this.readyState == 4) { req.onreadystatechange = null; if (this.status == 200) { var data = JSON.parse(this.response); } else { var error = JSON.parse(this.response).error; alert("Error retrieving Record – " + error.message); } } }; req.send(); }
  • RE: Transform DateTime field to Date javascript

    Hello, if the format of the string when you retrieve it is always the same "Date(xxxxx)", what I recomend is to get the number from the string and create a new Date type var and you pass to it the number that you get from the string, for example:

    var str = "Date(16456222000000)";

    var num = str.replace(/\D+/g, ""); // console.log(num) if you want to check the value

    var date = Date(num);

    console.log(date);

    Thanks!

    Community Support Team - Esteban

    If this Post helps, then please consider Accept as solution to help the other members find it more quickly.

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

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 Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans