Archive for August 21, 2011

Type-cast OData Service Datetime field in crm 2011

August 21, 2011 10 comments


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));

Hope it helps 🙂