0

I'm returning JSON via WCF using ResponseFormat = WebMessageFormat.Json from an SQL backend accessed with entity framework.

My query is returning

    {
  "DateInserted": "/Date(1435640400000-0500)/",
  "DateUpdated": "/Date(1454699734160-0600)/",

where DateInserted in my database is 2015-06-30 00:00:00.000 and DateUpdated in my database is 2016-02-05 13:15:34.160

but I haven't been able to find any information about what the numbers the Date() string represent, nor where the datetime is being converted to that string representation, so any information you may be able to share may be helpful to me.

Banjo Batman
  • 159
  • 11
  • 1
    It looks like a Unix timestamp. In this case Friday 5th February at 7:15pm GMT. I assume the 0500 is a time difference. It's the number of seconds since 01/01/1970 at midnight. Conversion is easy, add that number of seconds onto 01/01/1970. – Equalsk Feb 17 '16 at 15:57
  • 1
    For an explanation of this format see [ASP.NET AJAX: Inside JSON date and time string](https://msdn.microsoft.com/en-us/library/bb299886.aspx#intro_to_json_sidebarb). If you want a different format, see http://stackoverflow.com/questions/9266435/datacontractjsonserializer-deserializing-datetime-within-listobject – dbc Feb 17 '16 at 16:13
  • Thanks! Based on Equalsk's comment I was able to figure it out. Turns out, its the total number of milliseconds since 1/1/1970 at midnight, then subtract the second number of hours. – Banjo Batman Feb 17 '16 at 16:29

1 Answers1

2

DateTime values appear as WCF JSON strings in the form of "/Date(700000+0500)/", where the first number (700000 in the example provided) is the number of milliseconds in the GMT time zone, regular (non-daylight savings) time since midnight, January 1, 1970. The number may be negative to represent earlier times. The part that consists of "+0500" in the example is optional and indicates that the time is of the Local kind - that is, should be converted to the local time zone on deserialization. If it is absent, the time is deserialized as Utc. The actual number ("0500" in this example) and its sign (+ or -) are ignored.

The following Javascript function creates a new Javascript date using only the first part of the date string, e.g., number of milliseconds since midnight, January 1, 1970. The Javascript Date constructor will create a date with the correct locale time.

String.prototype.toDateFromWcfJsonString = function()
{
   return eval(this.replace(/\/Date\(([-|+]*\d+)([\+|-]\d+)*\)\//, "new Date($1)"));
};

See also http://msdn.microsoft.com/en-us/library/bb412170(v=vs.90).aspx.

Polyfun
  • 9,479
  • 4
  • 31
  • 39