Convert time to different timezone with jQuery

此生再无相见时 提交于 2019-12-11 02:27:08

问题


I have a time and date in the "Zulu" time zone (which is the same time zone as Coordinated Universal Time (UTC))

I need to convert this time and date into the Central time zone using jQuery.

Right now Im attempting to use "Moment Timezone" since I already use "Moment" and it seems like Timezone is capable of what I need but the documentation is sparse.

I thought this would work but it returns "Monday, January 20 2014 1:03 AM" without converting the time

var stampString='January 20 2014 01:03';
var m = moment(stampString);
var convertedTime=m.tz("CST6CDT").format('LLLL');

How can I accomplish what I need? Im open to using a different library if needed, possibly an ajax call to some php method? jQuery would be preferred though.


回答1:


You should not use CST6CDT. That is an old-style POSIX time zone. They're only there for backwards compatibility purposes. The correct zone for US Central Time is America/Chicago. There's a section about POSIX time zones in the timezone tag wiki if you want to learn more.

You'll also need to tell moment exactly what format you are using, and you'll want to tell it that it is being supplied in UTC.

var stampString = 'January 20 2014 01:03';
var m = moment.utc(stampString, "MMMM D YYYY HH:mm");
var convertedTime = m.tz("America/Chicago").format('LLLL');


来源:https://stackoverflow.com/questions/22451998/convert-time-to-different-timezone-with-jquery

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