TZIntl.js — International Date-Time Conversion

      
      
      
      		

      This is a demo widget for the use of TZIntl.js and TZProvider.js. Use mouse, cursor keys and/or initial letter keys to navigate.
      You may set an initial time zone by supplying a URL parameter (or hash fragment) "tz" as in "?tz=Europe/London".

      The Libraries

      TZIntl

      TZIntl is a lightweight and compatible abstraction of the Intl.DateTimeFormat interface for converting JS-dates to localized date-time objects. TZIntl provides the following static methods:

      Example

      function getTimeFor(tzString, date) {
          const tzDate = TZIntl.getDateTime(tzString, date);  // get date-time object
          const dd = TZIntl.dd;                               // returns a double-digit string
          const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
          let s = tzString || "Local Time";
          if (tzDate && !tzDate.error) {
              s += " ("
                 + tzDate.GMTOffsetName                  // evaluted GMT-based time zone
                 + ", "
                 + tzDate.offsetString                   // evaluted offset to UTC: +/-hh:mm
                 + ")\n" 
                 + weekdays[tzDate.weekday]
                 +  ", "
                 + tzDate.year
                 + "-"
                 + dd(tzDate.month)
                 + "-"
                 + dd(tzDate.day)
                 + " "
                 + dd(tzDate.hour)
                 + ":"
                 + dd(tzDate.minute)
                 + ":"
                 + dd(tzDate.second)
                 + " "
                 + tzDate.timeZoneName                   // short name computed by browser
                 + "\nISO: " + tzDate.toISOString();
          }
          else {
              s += ": " + (tzDate? tzDate.error : "[not implemented]");
          }
          return s;
      }
      
      const date = new Date("24 Dec 2021 04:30:20 UTC");
      
      console.log( date.toUTCString() );
      console.log( getTimeFor("America/New_York", date) );
      console.log( getTimeFor("Asia/Tokyo", date) );
      console.log( getTimeFor("Europe/London", date) );
      console.log( getTimeFor("UTC", date) );
      
      /*
      Fri, 24 Dec 2021 04:30:20 GMT
      
      America/New_York (GMT-5, -05:00)
      Thu, 2021-12-23 23:30:20 EST
      ISO: 2021-12-23T23:30:20-05:00
      
      Asia/Tokyo (GMT+9, +09:00)
      Fri, 2021-12-24 13:30:20 GMT+9
      ISO: 2021-12-24T13:30:20+09:00
      
      Europe/London (GMT, +00:00)
      Fri, 2021-12-24 04:30:20 GMT
      ISO: 2021-12-24T04:30:20Z
      
      UTC (GMT, +00:00)
      Fri, 2021-12-24 04:30:20 UTC
      ISO: 2021-12-24T04:30:20Z
      */
      
      console.log( getTimeFor("America/New_York") );
      // current time in New York (default current time)
      
      console.log( getTimeFor(null, date) );
      // date as local time (default time zone)
      

      TZProvider

      However, which time zone names are there, and which ones are actually supported by the browser? Notably, support may vary and only "UTC" is guaranteed to work. While there is an experimental method Intl.supportedValuesOf("timeZone") to enumerate supported time zone names, this is currently (as of Dec. 2021) supported solely by Mozilla/Firefox (beginning with release number 93).
      TZProvider provides a compact abstraction by using either the built-in enumeration or by testing the browser for any of the common and official IANA time zone names and enumerating those supported.

      TZProvider is an independent stand-alone library providing the following static methods:

      License

      MIT — do not strip heading comments.

      Copyright 2021 Norbert Landsteiner <www.masswerk.at>

      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

      The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

      Version

      1.1 — added support for time zone aliases.

      Download

      Author

      Norbert Landsteiner, 2021, www.masswerk.at

      If you want to support me: Buy Me A Coffee