How to create UTC date in JS?

假如想象 提交于 2019-12-02 16:45:24

问题


When i try to create the date in UTC like new Date(Date.UTC(2013, 10, 7, 10, 3, 19)) i still receive Thu Nov 07 2013 12:03:19 GMT+0200 (FLE Standard Time) where it adds +2 hours. How to create just 2013-11-07 10:03:19 ?


回答1:


The date you created is actually 2013-11-07 10:03:19 UTC, but when you print it, it will print the date in your local timezone. To extract the UTC date, you could try using toUTCString(), like so:

Date(Date.UTC(2013, 10, 7, 10, 3, 19)).toUTCString() //"Thu, 07 Nov 2013 10:03:19 GMT"



回答2:


Please remove Date.UTC:

new Date(Date.UTC(2013, 10, 7, 10, 3, 19))
Thu Nov 07 2013 15:33:19 GMT+0530 (IST)
new Date(2013, 10, 7, 10, 3, 19)
Thu Nov 07 2013 10:03:19 GMT+0530 (IST)



回答3:


Do you need toUTCString() ? As seen here: http://www.w3schools.com/jsref/jsref_toutcstring.asp



来源:https://stackoverflow.com/questions/19833205/how-to-create-utc-date-in-js

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