moment.js,时区和夏时制 [英] moment.js, timezones and daylight savings

查看:249
本文介绍了moment.js,时区和夏时制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从.Net服务返回UTC日期/时间列表,其格式如下:

I return a list of UTC dates/times from a .Net service, formatted like so:

"2013-07-09 19:48:07 +00:00".

在客户端上,我将每个字符串值转换为对应的基于UTC的时刻,就像这样

On the client, I convert each of these string values into a corresponding UTC-based moment, like so

var fooUtc = new moment.utc(serverDateTimeString)

在页面上,有一个下垂页面,其中包含用户可以更改的时区列表.它们与时区对象的集合绑定在一起,如下所示:

On the page, there is a droop-down containing a list of time-zones that the user can change. These are tied to a collection of time-zone objects like the following:

{
    id: "Central Standard Time",
    label: "(UTC-06:00) Central Time (US & Canada)",
    observesDaylightSavings: true,
    baseUtcOffset: {
        asHours: -6,
        asMinutes: -360,
        asText: "-06:00"
}

然后我显示经过所选时区偏移的每个时刻,如下所示:

I then display each moment passing in the selected time-zone offset, like so:

fooUtc.local().zone(selectedTimeZone.baseUtcOffset.asMinutes).format()

但是,结果没有考虑夏令时,因为来自.Net的时区数据无法区分dst和非dst偏移量.

However, the result does not take into account daylight savings, as the timezone data coming from .Net does not differentiate between dst and non dst offsets.

是否有一种方法可以使它们与moment.js或新的moment-timezone位一起使用?我认为,如果可以将标准UTC偏移量名称(例如:中央标准时间")映射到给定时区的 Olson 数据库标识符(例如:"America/Chicago"),但是如果有更简便的方法,请告诉我.

Is there a way to make this work with moment.js or the new moment-timezone bits? I think it could be possible if I could map the standard UTC offset names (ex: "Central Standard Time") to a given timezone's Olson DB identifier (ex: "America/Chicago"), but if there is an easier way, please let me know.

推荐答案

您应该在上使用 Noda Time 进行探索.网络端,并在客户端上 moment-timezone ,并传递IANA/Olson时区ID.

You should explore using Noda Time on the .Net side, and moment-timezone on the client, passing the IANA/Olson time zone id.

如果要保留下拉列表中的Windows时区ID,则可以使用Noda Time中嵌入的CLDR数据进行转换.我已经在这篇文章中记录了如何做到这一点:如何在Windows和IANA时区之间进行转换?

If you want to stick to the Windows time zone ids in your drop-down list, then you can do conversions with the CLDR data embedded in Noda Time. I have documented how to do it in this post: How to translate between Windows and IANA time zones?

但是更好的解决方案是避免一起使用Windows区域.您可以使用我在本文中介绍的技术来填充IANA/奥尔森ID列表:我应如何填充IANA/奥尔森时间列表野田时间的时区?

But the better solution would be to avoid the Windows zones all together. You can populate a list of IANA/Olson ids using the technique I describe in this post: How should I populate a list of IANA / Olson time zones from Noda Time?

更好的是,您可以将下拉列表替换为显示世界地图的控件(嵌入式或模态),以便您的用户可以轻松选择他们的时区.我所见过的最好的控件是这一个,但还有其他一些还有.

Better yet, you can replace your drop-down list with a control (inline or modal) that displays a map of the world, so your users can easily select their time zone. The best control for this that I have seen is this one, but there are a few others out there also.

如果您可以严格在IANA/Olson地区进行交易,则无需进行转化.您可以放弃Windows TimeZoneInfo对象,而只使用Noda Time.如果需要,您可以只替换时区转换功能,其余部分保持不变.或者,您可以全力以赴,用Noda Time类型替换所有DateTimeDateTimeOffset使用.由你决定.

If you can deal strictly in IANA/Olson zones, then there's no need for conversion. You can give up the Windows TimeZoneInfo object and just use Noda Time instead. If you want, you can replace just your time zone conversion functions and leave the rest intact. Or, you could go all-out and replace all of your DateTime and DateTimeOffset uses with Noda Time types. It's up to you.

这篇关于moment.js,时区和夏时制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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