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?

JmRag
  • 1,443
  • 7
  • 19
  • 56

3 Answers3

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

Community
  • 1
  • 1
michel.iamit
  • 5,788
  • 9
  • 55
  • 74
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

Afleg
  • 24
  • 1
0

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

See The "right" JSON date format.

Community
  • 1
  • 1
OlliM
  • 7,023
  • 1
  • 36
  • 47