How does ISO 8601 format date with timezone offset in JavaScript?

Purpose: Find local time and UTC time offset local time UTC time offset then build the URL in the following format.

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

The format is based on the W3C recommendation: http://www.w3.org/TR/xmlschema11-2/#dateTime

The documentation says:

For example, 2002-10-10T12: 00: 00-05: 00 (noon on October 10, 2002, central daylight saving time, and also standard eastern time in the USA) is 2002-10-10T17: 00: 00Z, five hours later than 2002-10-10T12: 00: 00Z.

Based on my understanding, I need to find my local time using the new Date () function, then use the getTimezoneOffset () function to calculate the difference, and then attach it to the end of the line.

1. Get local time in format

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

exit

 2013-07-02T09:00:00 

UTC 2.Get time offset per hour

 var offset = local.getTimezoneOffset() / 60; 

exit

 7 

3. Create URL (only temporary part)

 var duration = local + "-" + offset + ":00"; 

exit:

 2013-07-02T09:00:00-7:00 

The above conclusion means that my local time 2013/07/02 is 9am, and the difference with UTC is 7 hours (UTC is 7 hours ahead of local time)

So far this works, but what if getTimezoneOffset () returns a negative value, like -120?

I am wondering what the format should look like in this case, because I cannot understand from the W3C document. Thank you in advance.

+78
javascript timezone
Jul 02 '13 at 0:19
source share
5 answers

Below should work correctly, and for all browsers (thanks to @MattJohnson for a hint)

 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(); console.log(dt.toIsoString()); 
+141
Jul 02 '13 at 0:35
source share

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

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

In this format, UTC is represented by Z , and all other formats are represented by an offset from UTC. The value is the same as JavaScript, but the subtraction order is inverted, so the result bears the opposite sign.

Also, there is no method on a native Date object called format , so your function in # 1 will fail if you don't use the library to achieve this. See this documentation .

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 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.

+62
Jul 02 '13 at 0:58
source share

This is my function for customers time zone, it is weight and simple

  function getCurrentDateTimeMySql() { var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, 19).replace('T', ' '); var mySqlDT = localISOTime; return mySqlDT; } 
+2
May 25 '18 at 22:08
source share

Just sends my two here

I ran into this problem with datetime, so I did this:

 const moment = require('moment-timezone') const date = moment.tz('America/Bogota').format() 

Then save the date in the database to be able to compare it with any query.




To set moment-timezone

 npm i moment-timezone 
0
Feb 21 '19 at 23:48
source share

Check this:

 function dateToLocalISO(date) { const off = date.getTimezoneOffset() const absoff = Math.abs(off) return (new Date(date.getTime() - off*60*1000).toISOString().substr(0,23) + (off > 0 ? '-' : '+') + (absoff / 60).toFixed(0).padStart(2,'0') + ':' + (absoff % 60).toString().padStart(2,'0')) } // Test it: d = new Date() dateToLocalISO(d) // ==> '2019-06-21T16:07:22.181-03:00' // Is similar to: moment = require('moment') moment(d).format('YYYY-MM-DDTHH:mm:ss.SSSZ') // ==> '2019-06-21T16:07:22.181-03:00' 
0
Jun 21 '19 at 19:13
source share



All Articles