Quantcast
Channel: Getting the client's time zone (and offset) in JavaScript - Stack Overflow
Viewing all articles
Browse latest Browse all 34

Answer by Abrar Jahin for Getting the client's time zone (and offset) in JavaScript

$
0
0

Timezone in hours-

var offset = new Date().getTimezoneOffset();if(offset<0)    console.log( "Your timezone is- GMT+"+ (offset/-60));else    console.log( "Your timezone is- GMT-"+ offset/60);

If you want to be precise as you mentioned in comment, then you should try like this-

var offset = new Date().getTimezoneOffset();if(offset<0){    var extraZero = "";    if(-offset%60<10)      extraZero="0";    console.log( "Your timezone is- GMT+"+ Math.ceil(offset/-60)+":"+extraZero+(-offset%60));}else{    var extraZero = "";    if(offset%60<10)      extraZero="0";    console.log( "Your timezone is- GMT-"+ Math.floor(offset/60)+":"+extraZero+(offset%60));}

Viewing all articles
Browse latest Browse all 34

Trending Articles