Get the timezone of a date

2

I have this date:

2016-02-22T14:55:00.000-03:00

And I would like to take timezone:

-03:00

Automatically, without having to perform any regex.

    
asked by anonymous 21.07.2016 / 16:05

1 answer

4

If this date is returning as a string, you can use substr

var str = "2016-02-22T14:55:00.000-03:00";
var timezone = str.substr(str.length - 6);
console.log(timezone);

If the date is in timestamp format, you can do it as follows:

var offset = new Date("2016-02-22T14:55:00.000-03:00").getTimezoneOffset();

var o = Math.abs(offset);

var timezone = (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);

console.log(timezone);

The method getTimezoneOffset returns the difference, in minutes , from UTC to local time / date. Then it checks the value in minutes and converts to hours (dividing the value by 60) and formats the value readable as time and operator (+/-).

SoEn¹ Font

    
21.07.2016 / 16:14