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,232
  • 18
  • 69
  • 117

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
  • 994,864
  • 265
  • 3,241
  • 2,902
  • 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
  • 79,093
  • 40
  • 195
  • 263
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,401
  • 6
  • 60
  • 93
  • 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