4

i want to store timezone of client (visitor) which going to use my web portal. would you please show me way to find out timezone for client machine using JAVAScript code...

I need the GMT offset hours like `(GMT +5:30)`.
Vijay
  • 8,131
  • 11
  • 43
  • 69

4 Answers4

5

This could be a better approach to find client local time/offset

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

offset = ((offset<0? '+':'-')+ pad(parseInt(Math.abs(offset/60)), 2)+":"+pad(Math.abs(offset%60), 2));
alert(offset);

The output would be like = +05:30

rakhi4110
  • 9,253
  • 2
  • 30
  • 49
Mehul Dudhat
  • 421
  • 1
  • 7
  • 19
  • could you please mention, how this would fare better than the other approaches? – rakhi4110 Mar 14 '14 at 06:41
  • when you try with this code you can get direct different gmt time like +05:30 or -7:00 you can easily parse hours and minutes from this out put and create one calender object and that minutes and hours in that object and your time converted into local time. – Mehul Dudhat Mar 14 '14 at 13:43
3
var OffSetMinute = new Date().getTimezoneOffset();

OffSetMinute var will give the difference in minutes, between UTC and local time. The offset is positive if the local timezone is behind UTC and negative if it is ahead.

As said in comment by @Guillaume the above method is deprected so you can use following

  private static final Calendar cal =   new Calendar();
    private static final int LOCALTIMEZONEOFFSET = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60*1000); 

Calendar.ZONE_OFFSET gives you the standard offset (in msecs) from UTC. This doesn't change with DST. (Day Light Saving)

Calendar.DST_OFFSET gives you the current DST offset (in msecs) - if any. For example during summer in a country that uses DST this field is likely to have the value +1 hour (1000*60*60 msecs).

so just ADD int LOCALTIMEZONEOFFSET Value(if positive) or substarct the value (if your value is negative) into your UTC Time and that Will Give you client machine's time.

BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • 1
    Calendar.ZONE_OFFSET gives you the standard offset (in msecs) from UTC. is this will work now? – BhavikKama Jul 08 '13 at 07:06
  • Calendar.ZONE_OFFSET is the replacement for the deprecated getTimezoneOffset() but I'm not sure if the question is for Java or Javascript – Guillaume Jul 08 '13 at 07:09
  • @Bhavik Kama would you please describe me how last two row of your answer help me to retrive timezone for client(visitor)'s system. – Vijay Jul 08 '13 at 08:33
  • @Guillaume, [mozilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset) does not say that getTimezoneOffset is deprecated. How do you know it is deprecated? – Yevgeniy Afanasyev Jun 07 '18 at 05:31
2
function getTimezone() {
    var u = new Date().toString().match(/([-\+][0-9]+)\s/)[1];
    return u.substring(0, 3) + ':' + u.substring(3, u.length);
}
stefcud
  • 2,220
  • 4
  • 27
  • 37
1

code that works for me...

<script type="text/javascript" language="javascript" >
    function fnLoad()
    { 
        var objLocalZone = new Date();
        var strLocalZone=''+objLocalZone; 

        var mySplitResult = strLocalZone.split(" ");

        var newLocalZone = mySplitResult[5].slice(0,mySplitResult[5].length-2) +':'+mySplitResult[5].slice(mySplitResult[5].length-2,mySplitResult[5].length);
        document.getElementById("hdnTimeZone").value = newLocalZone;
        //alert('Length : '+newLocalZone);
    }
</script>

hidden input

<input type="hidden" id="hdnTimeZone" name="hdnTimeZone"/>

i don't know this is proper way to get client(visitor)'s timezone. but it works fine for me.If anybody have optimum solution this let me know. thanks...

Vijay
  • 8,131
  • 11
  • 43
  • 69