Home > CRM 2011, JScript > Type-cast OData Service Datetime field in crm 2011

Type-cast OData Service Datetime field in crm 2011


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 🙂

  1. September 21, 2011 at 6:40 AM

    Hi Rajeev,

    Very usefull article, helped me a lot.


  2. Lokesh Mishra
    November 3, 2011 at 3:23 PM

    Rearly a ncie post, spent hours searching before i bumped into it… thanks a lot for the solution cheerz 🙂

  3. Deepesh Somani
    March 21, 2012 at 5:45 PM

    It helped me also..Thanks rockstar..

  4. ASd
    May 29, 2013 at 3:19 PM

    That’s it. Thanks

  5. January 22, 2014 at 6:11 PM

    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! 🙂

    • January 23, 2014 at 12:34 PM

      Hi Naresh,

      Have u tried the query using OData Query Designer? I am able to get the records.

  6. January 23, 2014 at 4:56 PM

    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?

  7. dave
    October 30, 2014 at 1:29 AM

    time zone differences, i.e. it comes back from CRM as UTC

  8. amit
    January 1, 2015 at 1:43 PM

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

    Thanks in advance..

  1. March 27, 2013 at 2:19 AM

Leave a Reply to Lokesh Mishra Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: