Announcements
Hi guys, i query to get the date and time value of the field_validFrom and i check on the database, there's two data/record save on the database that is field_validFrom(this is the correct date and time) and field_validFromutc(incorrect one) but the data that i get was the utc datetime or maybe the conversion was wrong? and this is my conversion for the dateandtime
convertODataDateToODataString: function (crmDate) {
var date = new Date(parseInt(crmDate.replace("/Date(", "").replace(")/", ""), 10));
date = date.toISOString();
return date;
},
and this is the datetime save on my database the first one was field_validfrom and the second is field_validfromutc
but im sure that the data they get was the utc. Does anyone knows this?
Thanks for the help
*This post is locked for comments
This is caused by the way JavaScript Date object parses date strings. Take a look at this W3C page https://www.w3schools.com/js/js_date_formats.asp
The following returns (wrong) date (Tue Mar 24 2015 20:00:00 GMT-0400 (Eastern Daylight Time)
var d = new Date("2015-03-25");
However the following returns the correct date (Wed Mar 25 2015 08:00:00 GMT-0400 (Eastern Daylight Time)
var d = new Date("2015-03-25T12:00:00Z");
As per W3C:
Omitting T or Z in a date-time string can give different result in different browser.
UTC (Universal Time Coordinated) is the same as GMT (Greenwich Mean Time).
The solution that we came up with was to break the date string into it's various parts, year, month, ... and create the date by using the Date constructor rather than relying on the JS Date object string parser. This will always produce the correct date.
var d = new Date(yyyy,mm - 1,dd,hh,mm,ss);
Hope this helps.
this time i got invalid date. Thank you so much for the reply.
see if you are getting UTC DateTime for CreatedOn, and you can looking to convert to local time:
var createdOn=Xrm.Page.getAttribute("createdon").getValue();
var LocalDate=new Date(CreatedOn+ "UTC")
Local Date will give current Date and Time
Check some basics here : www.powerobjects.com/.../crm-2011-truths-about-datetime
If found useful, please mark the answer as verified
Regards,
Pranav
André Arnaud de Cal...
294,261
Super User 2025 Season 1
Martin Dráb
232,996
Most Valuable Professional
nmaenpaa
101,158
Moderator