用jQuery将时间转换到不同的时区 [英] Convert time to different timezone with jQuery

查看:1768
本文介绍了用jQuery将时间转换到不同的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在祖鲁时区(与世界协调时间(UTC)相同的时区)有时间和日期。



我需要转换这个时间和日期进入中央时区使用jQuery。



现在我试图使用时刻时区,因为我已经使用时刻,它似乎是时区我可以用我所需要的,但文件很稀少。



我以为这会工作,但是它返回2014年1月20日星期一1:03 AM,而不转换时间

  var stampString ='2014年1月20日01:03'; 
var m = moment(stampString);
var convertedTime = m.tz(CST6CDT)。format('LLLL');

如何完成我所需要的?我可以使用不同的库,如果需要,可能是一个ajax调用一些php方法? jQuery将是首选。

解决方案

您不应该使用 CST6CDT 。这是一个古老的POSIX时区。他们只是为了向后兼容的目的。美国中部时间的正确区域是 America / Chicago 。如果您想了解更多信息,请参阅时区标签wiki 中有关POSIX时区的部分。



你还需要准确地告诉你正在使用什么样的格式,你会想告诉它是以UTC提供的。

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


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.

解决方案

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');

这篇关于用jQuery将时间转换到不同的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆