36

Possible Duplicate:
Format a Microsoft JSON date?

The ASP.NET function Json() formats and returns a date as

{"d":"\/Date(1240718400000)\/"}

which has to be dealt with on the client side which is problematic. What are your suggestions for approaches to sending date values back and forth?

Community
  • 1
  • 1
ChrisP
  • 9,796
  • 21
  • 77
  • 121

7 Answers7

29

This was found in another post on Stack Overflow:

var date = new Date(parseInt(jsonDate.substr(6))); 

The substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor.

Community
  • 1
  • 1
Jimbo
  • 22,379
  • 42
  • 117
  • 159
11

If you are not tied to the MS JSON serializer you could use Json.NET. It comes with an IsoDateTimeConverter to handle issues with serializing dates. This will serialize dates into an ISO 8601 formatted string.

For instance, in our project serializing myObject is handled via the following code.

JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.Formatting = Formatting.Indented;
jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
jsonNetResult.Data = myObject;

If you decide to take the Json.NET plunge you'll also want to grab JsonNetResult as it returns an ActionResult that can be used in ASP.NET MVC application. It's quite easy to use.

For more info see: Good (Date)Times with Json.NET

Ryan Taylor
  • 8,740
  • 15
  • 65
  • 98
6

It may be ugly, but it works:

 var epoch = (new RegExp('/Date\\((-?[0-9]+)\\)/')).exec(d);
 $("#field").text((new Date(parseInt(epoch[1]))).toDateString());

Probably, it is not necessary to match the whole string, and just (-?[0-9]+) is enough...

Felix
  • 9,248
  • 10
  • 57
  • 89
5

Not everyone agrees with me that it's a good idea, but I find myself most often returning formatted strings instead of proper dates. See How I handle JSON dates returned by ASP.NET AJAX.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dave Ward
  • 59,815
  • 13
  • 117
  • 134
1

After playing with the Json.NET library, I'm wondering why you would choose to use the IsoDateTimeConverter over the JavascriptDateTimeConverter.

I found this to be easier to use with the Ext JS interfaces that I was using when serializing dates from an MVC Controller.

JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.Formatting = Formatting.Indented;
jsonNetResult.SerializerSettings.Converters.Add(new JavaScriptDateTimeConverter());
jsonNetResult.Data = myObject; 

I'm getting this data back into an Ext.data.JsonStore which is able to get the returned value as a date without me having to specify a date format to parse with.

        store:new Ext.data.JsonStore({
            url: pathContext + '/Subject.mvc/Notices',
            baseParams: { subjectId: this.subjectId },
            fields: [
               {name: 'Title'},
               {name: 'DateCreated', type: 'date' }
            ]
        }),

The JSON returned looks like this:

[{"Title":"Some title","DateCreated":new Date(1259175818323)}]

There isn't any reason to convert to ISO 8601 format and back if you don't have to.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brow-cow
  • 101
  • 1
  • 4
  • Because the `JavaScriptDateTimeConverter` [does not produce valid JSON](http://james.newtonking.com/json/help/?topic=html/DatesInJSON.htm). If you want to maintain interoperability with a wider range of clients that expect valid JSON, then you'd be better off sticking with ISO 8601. – Brian Rogers Nov 18 '13 at 01:00
1

Auto convert dates on the client side (if you use jQuery)

--

You didn't specify it, but since you're using ASP.NET MVC you could be using jQuery. If you do, converting to actual dates just became simpler if you use code provided on this blog post. The code extends jQuery's $.parseJSON() functionality, so it automatically converts ISO and ASP.NET date strings to actual JavaScript dates.

I use it with ASP.NET MVC, and it works like a charm. The best part is that it's also backwards compatible. Existing code that uses $.parseJSON() will work just like before (and actually work the same), but if you provide the second parameter and set its value to true, all dates will get automatically converted for you.

The extension uses native browser JSON support where applicable and also works in others that don't. Modern browsers support this functionality anyway.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
0

Take a look at the blog post jQuery, Ajax, ASP.NET and dates.

It details how to work with ASP.NET MVC and jQuery to pass dates via JSON between the server and client side.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex
  • 75,813
  • 86
  • 255
  • 348
  • This provides some insights, but deals w/ jQuery to ASP.NET web service rather than an MVC controller returning JsonResult via Json(). I am interested in jQuery to ASP.NET MVC w/o MSFT ajax. – ChrisP Aug 12 '09 at 01:08
  • The problem is primarily w/ complex objects that have a date property rather than a single date value. – ChrisP Aug 12 '09 at 01:10
  • The JSON format is the exact same in the example to what you described. Date Property or Single Date Value does not make a difference. – Alex Aug 12 '09 at 01:13