Type-cast OData Service Datetime field in crm 2011
Hi,
When you read any CRM DateTime value using OData Service, the value comes in “/Date(1314763200000)/“ format, which we can’t directly set to DateTime field.
Reason :-
- OData parses data in “Edm.DateTime” type
- <d:new_StartDate m:type=”Edm.DateTime”>2011-08-18T04:00:00Z</d:IBT_StartDate>
How to Type Cast to DateTime :-
- Use below code to type cast OData DateTime type (i.e., Edm.DateTime) to CRM DateTime
- var dt = ODataEntity.results[0].new_StartDate;
dt = dt.replace(“/Date(“, “”);
dt = dt.replace(“)/”, “”);
var dateValue = new Date(parseInt(dt, 10));
dateValue.setDate(dateValue.getDate());
Xrm.Page.data.entity.attributes.get(“{fld_name}”).setValue(dateValue);
Hope it helps 🙂
Categories: CRM 2011, JScript
/Date, CRM 2011, CRM date, CRM datetime, date conversion, date field, datetime field, Edm.DateTime, ODATA Date, type cast
Hi Rajeev,
Very usefull article, helped me a lot.
Congratulations!
Rearly a ncie post, spent hours searching before i bumped into it… thanks a lot for the solution cheerz 🙂
It helped me also..Thanks rockstar..
That’s it. Thanks
Hi Rajeev,
It’s very interesting. Can help me on this. Am trying to get the Appointments by startdate and end date. my query is bellow. but it’s giving results as empty
$filter=ScheduledStart eq datetime’2014-01-23T04:00:00′ and ScheduledEnd eq datetime’2014-01-23T08:30:00′ .
Can you help me. how to get the record! 🙂
Hi Naresh,
Have u tried the query using OData Query Designer? I am able to get the records.
Yes. It’s coming with le and ge operation, still issues persist with date time…
Am updating date time as 23/01/2013 4:30 PM. Its showing on form as same. But when call ODATA service it’s returning date time as ’24-01-2013T22:30:00Z’.
How it is possible?
time zone differences, i.e. it comes back from CRM as UTC
Hi Rajeev,
I retrieved value in UTC format but i want to add days in Retrieved Date i am doing something like this but when i add Days in retrieved then i am getting some unexpected results.
var shipmentdate = oppProduct[0].results[0].ap_shipmentdate;
var DateValue = new Date(parseInt(shipmentdate.replace(“/Date(“, “”).replace(“)/”, “”), 10))
var newdate = new Date().setDate(DateValue.getDate() + 5);
window.parent.Xrm.Page.getAttribute(“ap_testdate1”).setValue(newdate);
Thanks in advance..