简体   繁体   中英

Given a date/time and timezone, how do you convert that into a UTC timestamp?

I am calling an API that returns a date/time string such as "2014-04-30 15:32:01" . On top of this, I have a known timezone that this date/time exists in. I can see from the javascript Date() class has a .UTC() call for this, but that does not seem to accept a timezone as far as I can tell.

Given the date/time string + timezone, how can I convert those into a UTC timestamp?

我建议使用Moment.jsMoment Timezone

You can create a timestamp using Date.parse from your local time (RFC2822 date format to include the timezone), create a date from that and use Date.toUTCString to get the UTC time. Not sure if it'll work with day light savings though.

Example: http://jsfiddle.net/3vXV6/ (will alert the date)

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM