0

I am sending date values from C# application via JSON but instead of a standard date, it appears in this format /Date(1324512000000)/

Can anyone please tell me how to send it from C# in the format it expects? Thanks

InfoLearner
  • 14,952
  • 20
  • 76
  • 124

3 Answers3

3

That's how the JavaScriptSerializer is handling dates:

Date object, represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

You could convert this into a javascript Date like this:

var date = new Date(parseInt(jsonDate.substr(6)));
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • how do i send from C# in the form it expects? – InfoLearner Jan 09 '12 at 23:07
  • should i cast it to a string? – InfoLearner Jan 09 '12 at 23:07
  • @KnowledgeSeeker, no, in C# you don't need to do anything. In your controller action you simply `return Json(new { SomeDate = DateTime.Now });`. Then on the client you could invoke this controller action using AJAX and in the success callback if you wanted to manipulate the date as a javascript Date object you could do what I showed in my answer. – Darin Dimitrov Jan 09 '12 at 23:08
1

JSON doesn't recognize c#'s datetime object. You should send it back as a string by calling .toString on your datetime variable in your controller.

Travis J
  • 81,153
  • 41
  • 202
  • 273
0

That's just how dates are expressed in JSON. See here for how to parse it back into something useful in javascript: How do I format a Microsoft JSON date?

Community
  • 1
  • 1
Robert Levy
  • 28,747
  • 6
  • 62
  • 94
  • 1
    Why not have the server, which is way more efficient at this type of operation, handle the conversion? – Travis J Jan 09 '12 at 23:17