Why converting new.Date() .toISOString() changes the time?

允我心安 提交于 2019-12-04 01:46:30

In your this is inserting as Datetime block your slice are stripping of the timezone part (the Z at the end of toISOString output):

document.getElementById('clockinhour').value = mydate.toISOString().slice(0, 19).replace('T', ' ');

As pointed out by @RobG in the comments section, toISOString should always return the date in UTC (Z or +00:00).

RTFM: "The time zone [offset] is always UTC, denoted by the suffix Z",

The time "changes" because it is converted to UTC when you calls toISOString.

If you want to get ISO date in your timezone, you should take a look in these two questions: How to ISO 8601 format a Date with Timezone Offset in JavaScript? and How to format a JavaScript date

ISO time is time zone free. You'll notice with b you have time zone GMT-04:00 if you add those four hours to the 16 hours in the Date, you get 20

Just if someone else face the same issue and visit this question.

There is a solution here: https://stackoverflow.com/a/28149561

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!