
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(); }
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.