Same datetime across timezones in browser - on a booking engine

后端 未结 1 1362
情歌与酒
情歌与酒 2021-02-10 06:35

I\'m looking for the best practice/solution to book a service internationally, using same time in any browser. I don\'t quite get the logic (and dug around here too).

Us

相关标签:
1条回答
  • 2021-02-10 07:26

    There are a couple of ways you can go about this.

    Option 1

    • Forget about UTC, the browser's time zone, and the target time zone. Just treat them as unspecified datetimes.
    • Between server and client, don't pass integer ticks or seconds - those are bound to UTC. Instead, pass local datetimes as ISO8601 strings, without an offset or a 'Z'.
    • Use moment.js to parse and format the strings without introducing the browser's offset. For example:

      // to get a Date value suitable for use with an existing control or script.
      var date = moment('2013-04-19T14:00').toDate();
      
      // to get a string back from a Date that's ready to go to your server.
      var str = moment(date).format('YYYY-MM-DDTHH:mm');
      
    • On the server side, it depends what platform you are on. Probably ISO8601 is already supported. For example, in .Net it is simply .ToString("o"). If you need specific advise on this, please tell us about your server platform/language.

    Option 2

    • Transfer UTC datetimes to/from your server using ISO8601 with its 'Z' specifier.
    • Any time you work with the date, use a TZDB javascript library such as one of the ones I listed here. Convert to/from UTC and the target time zone - ignoring the browser's time zone.
    • This will work just fine, and now you're talking about a specific moment instead of some unspecified local time. However, those libraries require a lot of data and that is probably overkill for such a simple task. I don't recommend this unless you have many other conversions to do, such as converting the appointment time to other time zones.
    0 讨论(0)
提交回复
热议问题