0

When I serialize a date using Json.Net I get the date formatted as:

'2015-07-07T17:27:00.057'

once I receive that object from ajax it comes as a string not as a date. How can I convert '2015-07-07T17:27:00.057' to a javascript date.

I have tried

 new Date('2015-07-07T17:27:00.057') but that gives the incorrect date

Edit:

Found the problem. If I serialize DateTime.Now I get the wrong date. But if I serialize DateTime.UtcNow I get the correct date when deserializing. I need to save my dates in Coordinated Universal Time (UTC). The comments were the solution, thanks so much.

3
  • Usually, when a timezone is not specified, the computer system assumes GMT. For example, when I type that line into my browsers console, the date is July 7, 2015 11:27:00. Notice that the the JSON time is 17:27, but my browser interprets it as 11:27 because I am -6 from GMT. Commented Jul 7, 2015 at 22:13
  • 1
    Are you using e.g. DateTime.Now rather than DateTime.UtcNow? Timezones matter, your serialized value has a T in, rather than Z for universal time. (en.wikipedia.org/wiki/ISO_8601) Commented Jul 7, 2015 at 22:15
  • Yes I think that is the problem. I am using dateTime.Now :/ Commented Jul 7, 2015 at 22:18

3 Answers 3

2

Json.Net supports multiple ways of formatting a date, if your only consuming it from an ajax call you may want to look into JavaScriptDateTimeConverter.

http://www.newtonsoft.com/json/help/html/DatesInJSON.htm

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

Comments

1

The problem is that you need the timezone information encoded in the string. Try using DateTimeOffset.Now for a portable time format.

Also check out:

Comments

-1

try

new Date(JSON.parse(datestr));

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.