Get Timezone from the browser using angularjs

1

I need to get the timezone from the user's browser. As this is an application using angularjs, I decided to use the angular-moment library. However I did not succeed in getting the name of timezone.

What I need is a way in angularjs to get something like: 'America / Sao_Paulo', 'Europe / Paris'

I thought about using the library moment-timezone.js but I could not add it as a dependency in the angle, so I can use it in the controler.

    
asked by anonymous 01.10.2016 / 16:13

2 answers

1

I was able to resolve it as follows:

I added the JSTZ

<script src="js/jstz.min.js"></script>

The controler file includes the following code:

/ * jstz * /

(function () {
    'use strict';

....

})();

By adding JSTZ at the beginning of the file, I was able to gain access to library functions. So inside the controler I executed the following excerpt:

var timezone = jstz.determine();
 console.log(timezone.name()); 

Whose output was: "America / Sao_Paulo"

    
21.10.2016 / 03:39
-1

If you want the browser timezone formatted you can count on the JavaScript method (since angularjs is just a framework to work with) Date.toString and do:

var split = new Date().toString().split(" ");
var timeZoneFormatted = split[split.length - 2] + " " + split[split.length - 1];

This will return you "GMT-0400 (EST)" for example, including time zone minutes when applicable.

Alternatively, with regex you can extract any desired part:

For "GMT-0300 (EDT)":

new Date().toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1]

For "GMT-0300":

new Date().toString().match(/([A-Z]+[\+-][0-9]+)/)[1]

"EDT" only:

new Date().toString().match(/\(([A-Za-z\s].*)\)/)[1]

Only "-0300":

new Date().toString().match(/([-\+][0-9]+)\s/)[1]
Date.toString reference: 

Date.toString Reference:

link

    
05.10.2016 / 13:51