1

I'm using web API controller in my ASP.net MVC 4 project.

My simple scenario is:

I have a simple method that return a person object,that includes a string property and a date time property. On the client side, after getting this person object from the Web API method, I have a JSON object. Is it possible to convert the JSON date automatically to JavaScript date? I'm trying to avoid do it manually.


Just to be clear,

What I meant is doin it without:

var date = new Date(jsonDateStr);

After getting the object from the server, I want to avoid initializing the date property

with a java script date object as above.

Every time it's a different object, "person" was just an example.

BTW,

The JSON date, that I'm using is in ISO format, e.g: ""2013-07-16T08:50:47.193Z""

Thanks,

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
NadavSt
  • 113
  • 2
  • 10
  • How would you do it manually? Carve it on a stick? Just do var d = new Date(JSONTimeStamp) if it is a valid time since 1970 in milliseconds – mplungjan Jul 16 '13 at 08:27
  • what is the format of the jsondate – Arun P Johny Jul 16 '13 at 08:36
  • put some of your json in the question.EDIT – HIRA THAKUR Jul 16 '13 at 08:38
  • @mplungjan I might be missing something but .... `new Date(JSON.stringify(new Date()))` returns **"Invalid Date"**. – PA. Jul 16 '13 at 08:48
  • Because JSON.stringify wraps date in additional quotes: `JSON.stringify(new Date()) ""2013-07-16T08:50:47.193Z""`; this `new Date(JSON.stringify(new Date()).replace(/"/g, ""))` should work – Tommi Jul 16 '13 at 08:53
  • [Here](http://markembling.info/2011/07/json-date-time) is a good article with multiple options. – Tommi Jul 16 '13 at 08:54
  • @Tommi: You mean `str = JSON.stringify(new Date())` and then just `new Date(JSON.parse(str))`. No `replace` please. – Bergi Jul 16 '13 at 11:10
  • 1
    @user2125067: You might want to have a look at http://stackoverflow.com/q/15851877 – Bergi Jul 16 '13 at 11:12
  • Related: http://stackoverflow.com/questions/206384/how-to-format-a-json-date – demux Jul 16 '13 at 20:40
  • 1
    What you are wanting is to not have to convert the dates *explicitly*. The JSON standard doesn't handle javascript dates, so if you want to use javascript dates, the strings representing them will have to be converted. Now, how that happens is up to you. Maybe you are desiring something that uses .NET reflection to automatically construct something for the client-side that can handle the conversion for any object with a `DateTime` property , and that should be doable, but whether or not somebody has already built a library to do just that, I dunno. That might be cool project to build. – JayC Jul 16 '13 at 20:44
  • Also have a look at this fiddle [Fix iso dat parsing for IE/Safari](http://jsfiddle.net/mplungjan/QkasD/) – mplungjan Jul 17 '13 at 04:31
  • Thanks everyone, specially JayC. Yes JayC, what I need, is something like .net reflection on the client side. Can you mention a library that do so? – NadavSt Jul 17 '13 at 09:05

1 Answers1

0

What you are wanting is to not have to convert the dates explicitly. The JSON standard doesn't handle javascript dates, so if you want to use javascript dates, the strings representing them will have to be converted. Now, how that happens is up to you. Maybe you are desiring something that uses .NET reflection to automatically construct something for the client-side that can handle the conversion for any object with a DateTime property , and that should be doable, but whether or not somebody has already built a library to do just that, I dunno. That might be cool project to build.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265