Why does Date#getHours() return hour + 1?

纵然是瞬间 提交于 2020-06-22 12:18:30

问题


This is my code:

var feedDataTimestamp = new Date("2014-01-14T00:04:40+0000").getTime();
var parsedDate = new Date(+feedDataTimestamp);
alert(parsedDate.getHours());

but it should print 0, not 1: time is 00:04:40


回答1:


Because you (according to your Stackoverflow profile) are in Italy, so your time zone is UTC+1.

The time stamp you are inputting is UTC+0.

parsedDate will be in local time.

Use the getUTCHours() method if you want to get UTC time instead of local time.




回答2:


You set the timezone in the parsed string as +0000 so you seem to want the hours in UTC, use

alert(parsedDate.getUTCHours())


来源:https://stackoverflow.com/questions/21134980/why-does-dategethours-return-hour-1

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