Working with timezones and daylight savings time in Javascript

北战南征 提交于 2019-11-28 07:25:55

2 is a bad idea since, as you pointed out, it increases load times. If I were you I would do a combination of 1 and 3. I don't agree that this makes the JSON data messy or the REST interface non-optimal.

This is a classic tradeoff where to accept a bit more complexity in the protocol in order to simplify the client-side code.

Fast-forward to 2015, when it comes to formatting a date from other timezone, using specific locale, there are two options.

In the example, I use French locale and will use a (UTC+5) date of 27 March 2016, 2h15 which is not observed in western Europe (because of DST change, clock moves from 2h00 to 3h00) which is a common source of bugs.

For maximum portability, use moment.js library. Moment comes in with bundled 80+ locales.

  1. Using timestamp and UTC offset:

    moment(1459026900000).utcOffset(300).locale('fr').format("LLLL")
    // "dimanche 27 mars 2016 02:15"
    
  2. Using array of integers (note that in moment, month is 0-based, just like in native JS Date object)

    moment.utc([2016,3-1,27,2,15]).locale('fr').format("LLLL")
    // "dimanche 27 mars 2016 02:15"
    

You can test those methods by running code in browser's dev tools on http://momentjs.com/

The tricky part here is to use moment.utc which creates a moment date object with UTC flag, meaning that when that date is formatted, it will not be converted to user's timezone, but just displayed as-is (and since UTC does not observe DST, it is not prone to DST bug).

Future native solution: using Intl object

(note as of late 2015, this solution is supported by Chrome, Firefox, IE11 but still not supported in Safari 9)

Using array of integers:

    new Intl.DateTimeFormat('fr-FR', { timeZone: 'UTC', weekday: 'long',
          month: 'long', year: 'numeric', day: 'numeric', hour: 'numeric',
          minute: 'numeric' })
      .format(Date.UTC(2016,3-1,27,2,15))
    // "dimanche 27 mars 2016 02:15"

The key here is once again to use Date.UTC and timeZone: 'UTC' to make sure the provided date will not be converted to user's local timezone.


Note that in both cases we are using UTC methods, just to make sure the date will be used as-is without conversions. It's important though to realize those dates are fake UTC dates (they are representing time in a given arbitrary timezone, not UTC time), and should be only used for date formatting and displaying - should not be passed around.

have same problem. #1 is solution i guess. You should send all components of date (year, month, day, hours) at special container from client to server

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