When you call toJSON()
on a Date object it returns a string representing the Date using toISOString()
which conforms to the ISO8601 standard e.g. YYYY-MM-DDTHH:mm:ss.sssZ. The timezone is always UTC, hence the difference in your example.
Se here for more information of the Date object in Javascript.
There are a few things to mention with regards to your example:
- A date and time are locale specific, your browser establishes this based on your system clock (you can change this).
- When you serialise a date and time this locale information will be converted to a timestamp.
- If you were to persist this timestamp in a database and then reload this information into the browser using
Date.parse()
and then your browser would display this in the date and time correct for your current timezone e.g. India Standard Time.
I have created a simple jsFiddle to serve as an example. In my current timezone GMT, this serves an important example when looking at winterDate
and summerDate
, as in Summer in this timezone we enter daylight savings and advance time by one hour. What is important is that Javascript understands this and captures the timezone so the universal value of the time something occurred is preserved. If you change your system's timezone and then reload your browser you will be able to see this behaviour for yourself.
I hope this helps give you a bit more understanding of this behaviour, and why it is important.