最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - moment.js, timezones and daylight savings - Stack Overflow

programmeradmin2浏览0评论

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

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

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()

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

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.

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

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

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()

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

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.

Share Improve this question edited Jul 9, 2013 at 23:09 Joshua Barker asked Jul 9, 2013 at 23:00 Joshua BarkerJoshua Barker 1,0073 gold badges12 silver badges25 bronze badges 1
  • I ended up creating a mapper file based on the [this post][1] [1]: stackoverflow./questions/8372537/… – Joshua Barker Commented Jul 10, 2013 at 1:20
Add a ment  | 

1 Answer 1

Reset to default 6

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

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?

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.

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.

发布评论

评论列表(0)

  1. 暂无评论