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

javascript iso 8601 with timezone offset
javascript date format
javascript date to iso string without time
javascript date timezone
javascript date without timezone
toisostring is not a function
iso date now

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

var local = new Date().format("yyyy-MM-ddThh:mm:ss"); //today (local time)



2.Get UTC time offset by hour

var offset = local.getTimezoneOffset() / 60;



3.Construct URL (time part only)

var duration = local + "-" + offset + ":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 document. Thanks in advance.

The below should work properly, and for all browsers (thanks to @MattJohnson for the tip)

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

var dt = new Date();

Date.prototype.toISOString(), The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY - MM - DD T HH : mm : ss. sss Z or ± YYYYYY - MM - DD T HH : mm : ss. sss Z , respectively). The timezone is always zero UTC offset, as denoted by the suffix " Z ". If you want a datetime string set to some other date & time (not the current time), pass an appopriate parameter to the Date object. See Several ways to create a Date object for more. 3) Combine both DateTime and Timezone Offset. Concatenate datetime and timezone offset to get an ISO-8601 datetime string. console.log(current_datetime + timezone

getTimezoneOffset() returns the opposite sign of the format required by the spec that you referenced.

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

In this format, UTC is represented with a Z while all other formats are represented by an offset from UTC. The meaning is the same as JavaScript's, but the order of subtraction is inverted, so the result carries the opposite sign.

Also, there is no method on the native Date object called format, so your function in #1 will fail unless you are using a library to achieve this. Refer to this documentation.

If you are seeking 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 this:

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.

How to get DateTime with Timezone Offset (8601 format) in Javascript, Javascript has a toISOString method that gives a datetime in ISO-8601 format. But it gives datetime strings only in UTC format, YYYY-MM-DDTHH:mm:ss. sssZ. It does not give a timezone offset in hours and minutes. Home » Javascript » How to ISO 8601 format a Date with Timezone Offset in JavaScript? Tags: date, java, javascript, time. Related Posts. javascript – Angular

This is my function for the clients timezone, it's lite weight and simple

  function getCurrentDateTimeMySql() {        
      var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
      var localISOTime = (new Date( - tzoffset)).toISOString().slice(0, 19).replace('T', ' ');
      var mySqlDT = localISOTime;
      return mySqlDT;

What is this date format? 2011-08-12T20:17:46.384Z, is 15:09:17Z. Note that the Z letter without a space. How do I output an ISO 8601 formatted string in JavaScript? date and time to iso format in javascript-2. ISO 8601 format a Date with Timezone Offset in

I think it is worth considering that you can get the requested info with just a single API call to the standard library...

new Date().toLocaleString( 'sv', { timeZoneName: 'short' } );

// produces "2019-10-30 15:33:47 GMT−4"

You would have to do text swapping if you want to add the 'T' delimiter, remove the 'GMT-', or append the ':00' to the end.

But then you can easily play with the other options if you want to eg. use 12h time or omit the seconds etc.

Note that I'm using Sweden as locale because it is one of the countries that uses ISO 8601 format. I think most of the ISO countries use this 'GMT-4' format for the timezone offset other then Canada which uses the time zone abbreviation eg. "EDT" for eastern-daylight-time.

You can get the same thing from the newer standard i18n function "Intl.DateTimeFormat()" but you have to tell it to include the time via the options or it will just give date.

UTC Time Now, century (1900 + year) . For example, 95 is converted to the year 1995 . The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively). The timezone is always zero UTC offset, as denoted by the suffix "Z".

Just my two sends here

I was facing this issue with datetimes so what I did is this:

const moment = require('moment-timezone')

const date ='America/Bogota').format()

Then save date to db to be able to compare it from some query.

To install moment-timezone

npm i moment-timezone

ISO 8601, The toISOString() method converts a Date object into a string, using the ISO standard. The standard is called ISO-8601 and the format is: YYYY-MM-DDTHH:​mm:ss  Given a non-standard date string of "March 7, 2014", parse() assumes a local time zone, but given a simplification of the ISO 8601 calendar date extended format such as "2014-03-07", it will assume a time zone of UTC (ES5 and ECMAScript 2015).

Date.UTC(), I pretty well knew that the time zone support in JavaScript is… Offset. +09:00 in UTC+09:00 means the local time is 9 hours ahead than the UTC In fact, the start and end dates of DST are all different, varying country by country. You can create strings of the ISO-8601 format using the toISOString() or toJSON() method. The function above omits time zone offset information (except if local time happens to be UTC), so I use the function below to show the local offset in a single location. You can also append its output to results from the above function if you wish to show the offset in each and every time:

JavaScript toISOString() Method, When the time zone offset is absent, date-only forms are interpreted as a UTC time and​started/ Support for ISO 8601 compliant zoned date times #64. Open. ISO 8601 Data elements and interchange formats – Information interchange – Representation of dates and times is an international standard covering the exchange of date - and time -related data. It was issued by the International Organization for Standardization (ISO) and was first published in 1988. The purpose of this standard is to

Handling Time Zone in JavaScript - TOAST UI, Working with dates in JavaScript can be complicated. Learn all the In any of these cases, the resulting date is relative to the timezone of your computer. This means that two toISOString() //"2018-07-22T05:22:13.000Z" (ISO 8601 format) date.toLocaleString() setTimezoneOffset(newValue). setDay and  A valid global date and time string is the same format as a local date and time string, except it has a time zone string appended to the end, following the time. Time zone offset string. A time zone offset string specifies the offset in either a positive or a negative number of hours and minutes from the standard time base.

  • The sign indicates the offset of the local time from GMT
  • @masato-san You have to invert the sign, see the definition at…
  • Here pad function returns proper string for each section of date except milliseconds. For instance, for 5ms as an input will return 05, which supposed to be 005. Here is a link with minimum modified version of the same function jsbin
  • Please note: there is a subtle bug in this answer. Timezone offset will not be calculated correctly if the zone had offset minutes and was negative (eg. -09:30), such as certain locations in France and Canada. Mod returns a negative number, so doing floor() before abs() inadvertently made the offset MORE negative. To correct this bug, so abs() and THEN floor(). Tried to edit the answer, but apparently "This edit was intended to address the author of the post and makes no sense as an edit".
  • @tytk - Great catch! That is, indeed, subtle. I added your fix to the answer. Thanks for commenting!
  • Using toISOString() won't work. The +01:00 format requires the time part be local time. toISOString() would give a UTC time string.
  • @AustinFrance - You're right! I'm surprised I made that mistake at the time, as I correct others on this point often. Sorry I didn't see your comment two years ago! Answer edited.
  • @tsh, are you sure? You are getting the current time offset, which is later used to simulate the local time. Maybe at that point the offset was different, but it doesn't matter because you just care about now.