Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

javascript - How can I convert a date to GMT?

How to convert date of different timezones to GMT 0? Let say I have dates like this

Fri Jan 20 2012 11:51:36 GMT-0500
Fri Jan 20 2012 11:51:36 GMT-0300
Fri Jan 20 2012 11:51:36 GMT+0500

to

Fri Jan 20 2012 06:51:36 GMT
Fri Jan 20 2012 08:51:36 GMT
Fri Jan 20 2012 16:51:36 GMT

these are dates I am getting via new Date().toString() on different timezones. Is there any js method to detect timezone and convert accordingly?

EDIT: The second three dates are not the GMT converted times of the first three. For example, Fri Jan 20 2012 11:51:36 GMT-0500 in GMT is Fri Jan 20 2012 16:51:36 GMT not Fri Jan 20 2012 06:51:36 GMT. GMT-0500 is behind GMT, not ahead, so you have to add the offset to get GMT, not subtract it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Matt Johnson-Pint's answer is much better than this one was.

console.log(new Date("Fri Jan 20 2012 11:51:36 GMT-0500").toUTCString())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...