0

Hello I have an object in js with a field date. I try to stringify it at an ajax request but the result is inconsistent. After stringify the new object is one day earlier.

To be more specific this is the code on my file:

console.log(reservation.checkin);
console.log( JSON.stringify(reservation.checkin));

And this is the outcome:

Thu Jan 01 2015 00:00:00 GMT+0200 (EET)
"2014-12-31T22:00:00.000Z"

Am I doing something wrong? Is that output what it should be? Thx in advance!

edit: From an answer below it seems that it is at different timezone. What is the correct way to stringify this date?

2
  • reservation.checkin.toLocaleString() Commented Mar 26, 2015 at 11:03
  • Why is this a problem? You should show your code that uses that timestamp, you should be able to convert it to whatever timezone you need Commented Mar 26, 2015 at 11:04

3 Answers 3

1

It's not changing the date, just showing it in another timezone (UTC / GMT)

GMT+0200 (EET) means 2 hours differenece with UTC / GMT

so that's exaclty what you see in the result.

it depends a bit on the purpose. If you want ot post this in another API, it should work fine (presuming, the api uses timezone standards), in case you want to just show it in a gui... why use the json stringify...

I am not going to do all the math for you, i suggest you know why now, so just google: 'javascript format timezone' or something like that.

e.g: Convert date to another timezone in JavaScript

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

3 Comments

I see. Thx for the answer, but how can I fix this?
Now I am more confused. How is python related to js-jquery?
sorry did not see it was about javascript, but does not matter just search for formatting javascript and timezone.
0

Well, it is timezone - your date is GMT +2:00, and after applying Stringify you get UTC. You may want to check Date method .getTimezoneOffset() and maybe update the date

Comments

0

JSON.stringify is calling Date.toJSON() to convert the date.

See The "right" JSON date format.

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.