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,

12
  • 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 Commented Jul 16, 2013 at 8:27
  • what is the format of the jsondate Commented Jul 16, 2013 at 8:36
  • put some of your json in the question.EDIT Commented Jul 16, 2013 at 8:38
  • 1
    @user2125067: You might want to have a look at stackoverflow.com/q/15851877 Commented Jul 16, 2013 at 11:12
  • 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. Commented Jul 16, 2013 at 20:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.