OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to ISO 8601 format a Date with Timezone Offset in JavaScript?

  • Thread starter Thread starter Meow
  • Start date Start date
M

Meow

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

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

The format is based on the W3C recommendation. The documentation says:

For example, 2002-10-10T12:00:00−05:00 (noon on 10 October 2002, Central Daylight Savings Time as well as Eastern Standard Time in the U.S.) is equal to 2002-10-10T17:00:00Z, five hours later than 2002-10-10T12:00:00Z.

So based on my understanding, I need to find my local time by new Date() then use getTimezoneOffset() function to compute the difference then attach it to the end of string.


  1. Get local time with format

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

  2. Get UTC time offset by hour

    Code:
    var offset = local.getTimezoneOffset() / 60; // 7

  3. Construct URL (time part only)

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

The above output means my local time is 2013/07/02 9am and difference from UTC is 7 hours (UTC is 7 hours ahead of local time)

So far it seems to work but what if getTimezoneOffset() returns negative value like -120?

I'm wondering how the format should look like in such case because I cannot figure out from W3C documentation.

<p><strong>Goal:</strong> Find the <code>local time</code> and <code>UTC time offset</code> then construct the URL in following format.</p>
<p>Example URL: <code>/Actions/Sleep?duration=2002-10-10T12:00:00−05:00</code></p>
<p>The format is based on the <a href="http://www.w3.org/TR/xmlschema11-2/#dateTime" rel="noreferrer">W3C recommendation</a>. The documentation says:</p>
<blockquote>
<p>For example, 2002-10-10T12:00:00−05:00 (noon on 10 October 2002,
Central Daylight Savings Time as well as Eastern Standard Time in the U.S.)
is equal to 2002-10-10T17:00:00Z, five hours later than 2002-10-10T12:00:00Z.</p>
</blockquote>
<p>So based on my understanding, I need to find my local time by <code>new Date()</code> then use <code>getTimezoneOffset()</code> function to compute the difference then attach it to the end of string.</p>
<ol>
<li><p>Get local time with <code>format</code></p>
<pre><code>var local = new Date().format("yyyy-MM-ddThh:mm:ss"); // 2013-07-02T09:00:00
</code></pre>
</li>
<li><p>Get UTC time offset by hour</p>
<pre><code>var offset = local.getTimezoneOffset() / 60; // 7
</code></pre>
</li>
<li><p>Construct URL (time part only)</p>
<pre><code>var duration = local + "-" + offset + ":00"; // 2013-07-02T09:00:00-7:00
</code></pre>
</li>
</ol>
<p>The above output means my local time is 2013/07/02 9am and difference from UTC is 7 hours (UTC is 7 hours ahead of local time)</p>
<p>So far it seems to work but what if <code>getTimezoneOffset()</code> returns negative value like -120?</p>
<p>I'm wondering how the format should look like in such case because I cannot figure out from W3C documentation.</p>
 

Latest posts

Top