How to get timezone offset from string in javascript

I get the date in a string format with an offset, but javascript converts it to the local device time

var d = new Date("2012-11-13T11:34:58-05:00"); debug.log(d); 

Returns Tue Nov 13 2012 17:34:58 GMT + 0100 (CET)

 var offset = d.getTimezoneOffset(); debug.log(offset); 

returns -60 (my device is utc + 1h)

I just want to have time with an offset or have the time zone offset indicated on the line (-5h in the example)

+4
source share
2 answers

Well, the only solution I found is to create my custom time object by parsing the line

 //ex: 2012-11-13T10:56:58-05:00 function CustomDate(timeString){ var completeDate = timeString.split("T")[0]; var timeAndOffset = timeString.split("T")[1]; //date this.year = completeDate.split("-")[0]; this.month = completeDate.split("-")[1]; this.day = completeDate.split("-")[2]; this.date = this.year + "/" + this.month + "/"+this.day; //negative time offset if (timeAndOffset.search("-") != -1){ var completeOffset = timeAndOffset.split("-")[1]; this.offset = parseInt(completeOffset.split(":")[0]) * -1; var originalTime = timeAndOffset.split("-")[0]; this.hours = parseInt(originalTime.split(":")[0]); this.minutes = parseInt(originalTime.split(":")[1]); this.seconds = parseInt(originalTime.split(":")[2]); this.time = this.hours + ":" + this.minutes + ":"+this.seconds; } ///positive time offset else if (timeAndOffset.search(/\+/) != -1){ var completeOffset = timeAndOffset.split("+")[1]; this.offset = parseInt(completeOffset.split(":")[0]); var originalTime = timeAndOffset.split("+")[0]; this.hours = parseInt( originalTime.split(":")[0]); this.minutes = parseInt(originalTime.split(":")[1]); this.seconds = parseInt(originalTime.split(":")[2]); this.time = this.hours + ":" + this.minutes + ":"+this.seconds; } //no time offset declared else{ this.hours = parseInt(timeAndOffset.split(":")[0]); this.minutes = parseInt(timeAndOffset.split(":")[1]); this.seconds = parseInt(timeAndOffset.split(":")[2]); this.offset = 0; this.time = this.hours + ":" + this.minutes + ":"+this.seconds; } } 

For example, if I want to show what time is received on 2012-11-13T11: 34: 58-05: 00 at the specified time zone offset:

 var aDate = new CustomDate("2012-11-13T11:34:58-05:00"); alert("date: " + aDate.date +" time: "+aDate.time+" offset: "+aDate.offset); 

and i get

 date: 2012/11/13 time: 11:34:58 offset: -5 

The problem with this solution is that date and time conventions are manually defined in the code, so they will not be automatically adapted to the user language.

+1
source

I'm not quite sure that I know what you are asking, but I do not think javascript can provide an offset other than local because it does not know where the time you gave it happened ..

So, you got this "2012-11-13T11: 34: 58-05: 00", but there is no information about where this time came from, it can be time anywhere in the world, so by default it does not depend on your local time zone if for offset.

0
source

Source: https://habr.com/ru/post/1445824/


All Articles