Home  >  Q&A  >  body text

How to format a date with time zone offset using ISO 8601 in JavaScript?

Goal: Find the local time and UTC time offset , and then construct the URL in the following format.

Example URL:/Actions/Sleep?duration=2002-10-10T12:00:00−05:00

This format is based on W3C recommendations. The documentation says:

For example, 2002-10-10T12:00:00−05:00 (at noon on October 10, 2002, Central Daylight Time and Eastern Standard Time) Equal to 2002-10-10T17:00:00Z, which is five hours later than 2002-10-10T12:00:00Z.

So from my understanding I need to find the local time via new Date() and then calculate the difference using the getTimezoneOffset() function and then append it to the end of the string .

  1. Use format to get the local time

    var local = new Date().format("yyyy-MM-ddThh:mm:ss"); // 2013-07-02T09:00:00
  2. Get UTC time offset

    var offset = local.getTimezoneOffset() / 60; // 7
  3. Construct URL (time part only)

    var duration = local + "-" + offset + ":00"; // 2013-07-02T09:00:00-7:00

The above output means that my local time is 9am on July 2, 2013, which is 7 hours different from UTC (UTC is 7 hours ahead of local time)

So far it seems to work, but what if getTimezoneOffset() returns a negative value (e.g. -120)?

I'm wondering what the format should look like in this case, since I can't figure it out from the W3C documentation.

P粉054616867P粉054616867207 days ago675

reply all(2)I'll reply

  • P粉842215006

    P粉8422150062024-03-26 13:16:45

    getTimezoneOffset() Returns the opposite sign of the format required by the specification you reference.

    This format is also known as ISO8601, or more accurately RFC3339.

    In this format, UTC is represented by Z, while all other formats are represented by an offset from UTC. The meaning is the same as in JavaScript, but the order of subtraction is reversed, so the result has the opposite sign.

    Additionally, there is no method named format on the local Date object, so the function in #1 will fail unless you use a library for this purpose. See this document.

    If you are looking for a library that can work with this format directly, I recommend trying moment.js. In fact, this is the default format, so you can simply do:

    var m = moment();    // get "now" as a moment
    var s = m.format();  // the ISO format is the default so no parameters are needed
    
    // sample output:   2013-07-01T17:55:13-07:00

    This is a well-tested cross-browser solution and has many other useful features.

    reply
    0
  • P粉545956597

    P粉5459565972024-03-26 12:26:52

    This is a simple helper function that formats JS dates for you.

    function toIsoString(date) {
      var tzo = -date.getTimezoneOffset(),
          dif = tzo >= 0 ? '+' : '-',
          pad = function(num) {
              return (num < 10 ? '0' : '') + num;
          };
    
      return date.getFullYear() +
          '-' + pad(date.getMonth() + 1) +
          '-' + pad(date.getDate()) +
          'T' + pad(date.getHours()) +
          ':' + pad(date.getMinutes()) +
          ':' + pad(date.getSeconds()) +
          dif + pad(Math.floor(Math.abs(tzo) / 60)) +
          ':' + pad(Math.abs(tzo) % 60);
    }
    
    var dt = new Date();
    console.log(toIsoString(dt));

    reply
    0
  • Cancelreply