Getting the client's timezone offset in JavaSc

2018-12-31 01:40发布

How can I gather the visitor's time zone information? I need the Timezone, as well as the GMT offset hours.

23条回答
唯独是你
2楼-- · 2018-12-31 01:59

It's already been answered how to get offset in minutes as an integer, but in case anyone wants the local GMT offset as a string e.g. "+1130":

function pad(number, length){
    var str = "" + number
    while (str.length < length) {
        str = '0'+str
    }
    return str
}

var offset = new Date().getTimezoneOffset()
offset = ((offset<0? '+':'-')+ // Note the reversed sign!
          pad(parseInt(Math.abs(offset/60)), 2)+
          pad(Math.abs(offset%60), 2))
查看更多
一个人的天荒地老
3楼-- · 2018-12-31 02:00

I was looking for just the 3 letter string (like "PDT") and tried Marquez's answer but had to tweak it a bit for cross browser support. Luckily, the string is the last potential match :)

Here's what I did, in CoffeeScript:

browserTimezone = ->
  userDate = new Date()
  zoneMatches = userDate.toString().match(/([A-Z][A-Z][A-Z])/g)
  userZone = zoneMatches[zoneMatches.length - 1]

Works in IE8, IE9, Safari 9, Firefox 44, and Chrome 48

查看更多
梦寄多情
4楼-- · 2018-12-31 02:03

This value is from user's machine and it can be changed anytime so I think it doesn't matter, I just want to get an approximate value and then convert it to GMT in my server.

For example, I am from Taiwan and it returns "+8" for me.

Working example

JS

function timezone() {
    var offset = new Date().getTimezoneOffset();
    var minutes = Math.abs(offset);
    var hours = Math.floor(minutes / 60);
    var prefix = offset < 0 ? "+" : "-";
    return prefix+hours;
}


$('#result').html(timezone());

HTML

<div id="result"></div>

Result

+8
查看更多
美炸的是我
5楼-- · 2018-12-31 02:04

You just to to include moment.js and jstz.js

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.min.js"></script>

and after that

<script>
$(function(){
 var currentTimezone = jstz.determine();
 var timezone = currentTimezone.name();
 alert(timezone);
});

</script>
查看更多
栀子花@的思念
6楼-- · 2018-12-31 02:06

I wrote a function in my project, which returns the timezone in hh:mm format. I hope this may help someone:

function getTimeZone() {
    var offset = new Date().getTimezoneOffset(), o = Math.abs(offset);
    return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);
}

// Outputs: +5:00

function getTimeZone() {
  var offset = new Date().getTimezoneOffset(), o = Math.abs(offset);
  return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);
}


// See output
document.write(getTimeZone());

Working Fiddle

查看更多
孤独总比滥情好
7楼-- · 2018-12-31 02:09

See this resultant operator was opposite to the Timezone .So apply some math function then validate the num less or more.

enter image description here

See the MDN document

var a = new Date().getTimezoneOffset();

var res = -Math.round(a/60)+':'+-(a%60);
res = res < 0 ?res : '+'+res;

console.log(res)

查看更多
登录 后发表回答