2016-04-04 50 views
1

我使用moment.js + timezone处理日期和时间对象,并使用全球化向用户显示数字,货币等。使用jquery全球化格式化一个moment.js +时区

现在我想用全球化打印一个moment.js日期,所以我的第一种方法是使用getDate() - moment.js函数,丢弃时区信息(Ex 1)。当我将日期值转换为正确的时区时初始化一个新的Date(),我得到正确的结果(例2),但是当我使用完整版时,它显示UTC时区(显然不正确)。有什么办法可以让这些组件一起工作吗?

var m = moment.tz(1459779436, 'Europe/Berlin'); 
// Ex 1: Date is printed in UTC, so with an offset of 2 hours 
globalize.formatDate(m.getDate(), {datetime: 'full'}); 
globalize.formatDate(new Date(m.toISOString()), {datetime: 'full'}); 
// Ex 2: Correct times are printed, but with wrong timezone 
globalize.formatDate(new Date(m.year(), m.month(), m.date(), m.hour(), m.minute(), m.seconds(), m.milliseconds()), {datetime: 'full'}); 

回答

0

全球化将在即将到来的1.3版上支持IANA时区!

npm install [email protected] cldr-data iana-tz-data 

然后

var Globalize = require("globalize"); 
Globalize.load(require("cldr-data").entireSupplemental()); 
Globalize.load(require("cldr-data").entireMainFor("en")); 
Globalize.loadIANATimezone(require("iana-tz-data")); 

Globalize("en").formatDate(new Date(), {datetime: "short", timeZone: "America/Los_Angeles"}); 
// > '3/19/17, 3:19 PM' 
Globalize("en").formatDate(new Date(), {datetime: "short", timeZone: "America/New_York"}); 
// > '3/19/17, 6:19 PM' 
Globalize("en").formatDate(new Date(), {datetime: "short", timeZone: "America/Sao_Paulo"}); 
// > '3/19/17, 7:19 PM' 
Globalize("en").formatDate(new Date(), {datetime: "short", timeZone: "Europe/Berlin"}); 
// > '3/19/17, 11:19 PM' 

Globalize("en").formatDate(new Date(), {datetime: "full", timeZone: "America/Los_Angeles"}); 
// > 'Sunday, March 19, 2017 at 3:19:22 PM Pacific Daylight Time' 
Globalize("en").formatDate(new Date(), {datetime: "full", timeZone: "America/New_York"}); 
// > 'Sunday, March 19, 2017 at 6:19:22 PM Eastern Daylight Time' 
Globalize("en").formatDate(new Date(), {datetime: "full", timeZone: "America/Sao_Paulo"}); 
// > 'Sunday, March 19, 2017 at 7:19:22 PM Brasilia Standard Time' 
Globalize("en").formatDate(new Date(), {datetime: "full", timeZone: "Europe/Berlin"}); 
// > 'Sunday, March 19, 2017 at 11:19:53 PM Central European Standard Time' 

PS:注意它也支持解析特定IANA时区的日期字符串。这里

详细https://github.com/globalizejs/globalize/pull/701#issuecomment-287652673


老答案:

你可以使用moment.js日期操纵和使用Globalize的格式化的日期没有时区部分,即,你有{datetime: "medium"}第二个示例[ 1]。这对你是否可行?或者,你可以使用那个加上moment.js打印你的时区名称吗?

1:请注意,如果您需要不同/自定义模式,则可以使用{skeleton: <pattern>}

正确答案

全球化的时区的支持仅限于由本地JavaScript日期,其时区的支持仅限于获取用户的本地时区偏移,这一切,即怎样提供的年代,它不允许设置日期与不同的时区名称或时区偏移量。因此,它只允许格式化用户的本地时区(不是任意的)。因此,我认为全球化对你的日期和时间格式有用,但不是时区的一部分。如果您想了解更多关于全球化时区支持的信息,请点击https://github.com/jquery/globalize/pull/202#issuecomment-33020199

使用Globalize格式化日期将为您提供符合CLDR的格式(以及最新的语言环境数据),它将为您提供运行时支持:小的最终文件大小(用于更快的页面加载)和预编译的格式化程序客户端执行速度更快)(more info)。我希望这可以提供一些帮助,如果您还有其他问题,请告诉我。

+0

好的,谢谢你的解释,这个主要是我想到的。我在节点应用程序中使用这两个组件,以允许客户使用可选模板格式化他们的日期(在奇怪的时区),所以我喜欢选择使用date/time/datetime + short/meidum/long/full模板的可能性的全球化,但我不得不使用另一种解决方案,我猜。不过谢谢! –

0

我现在使用了一个非常冒险的方法,只是用一个函数返回由时区时区生成的值来替换新日期的getTimezoneOffset。

function momentToDate(input) { // Do NOT use the result to anything else than passing it into globalize 
    var res = new Date(input.year(), input.month(), input.date(), input.hour(), input.minute(), input.seconds(), input.milliseconds()); 
    var offset = input.utcOffset(); 
    res.getTimezoneOffset = function() { 
     return offset; 
    } 
    return res; 
} 

因此,全球化将打印正确的日期和正确的时区偏移量。