3

My application is hosted at one place whose Timezone is "A". And user can login from anywhere. Let's say he/she logged in from place whose timezone "B". I want the timezone of User from where he/she get logged in. So I can display him dates in his/her timezone.

So is there any way to get that user's timezone?

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Rajnikant Patel
  • 561
  • 3
  • 19
  • possible duplicate of [how to detect users timezone](http://stackoverflow.com/questions/16525617/how-to-detect-users-timezone) – Matt Johnson-Pint Jul 31 '13 at 17:32
  • See specifically [my answer](http://stackoverflow.com/a/16526897/634824) to that question. While you tagged your question for GWT - the answers are the same. – Matt Johnson-Pint Jul 31 '13 at 17:33

3 Answers3

2

A way to retrieve the client's timezone is to use javascript code through a native JNI method, as following:

private native int getClientOffsetTimeZone() /*-{
    return new Date().getTimezoneOffset();
}-*/;

Note that the getTimezoneOffset() method returns the time difference between UTC time and local time, in minutes.

For example, If your time zone is GMT+2, -120 will be returned.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Yeser Amer
  • 111
  • 1
  • 10
1

GWT has the com.google.gwt.core.client.JsDate library, which is a wrapper to the JSNI that Yeser mentioned. That library exposes the native JavaScript Date object, which allows you to use getTimezoneOffset().

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Jason Washo
  • 536
  • 6
  • 22
0

From client, you can retrieve the timezone offset with Date.getTimezoneOffset() and pass it to your server then use this offset to create correct Dates.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
user2508244
  • 91
  • 2
  • 7
  • If the date formatting is done on the client-side, you could also just exchange timestamps from UTC (`Date#getTime()` and `new Date(long)`) – Thomas Broyer Jul 31 '13 at 13:24
  • @ThomasBroyer - That is true, but that's not what was asked. – Matt Johnson-Pint Jul 31 '13 at 22:26
  • 1
    @user2508244 - A time zone is not an offset. Please read [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info). With the approach you are suggesting, the only guarantee you can make is for a single moment in time. Offsets change, even in a single time zone. – Matt Johnson-Pint Jul 31 '13 at 22:28
  • @MattJohnson and that's why I didn't post it as an answer ;-) – Thomas Broyer Aug 01 '13 at 10:31
  • But a timezone offset is used to create a timezone. This is from the timezone-js page at GitHub: https://github.com/mde/timezone-js
    dt_str_tz is a date string containing timezone information, i.e. containing Z, T or a timezone offset matching the regular expression /[+-][0-9]{4}/ (e.g. +0200).
    – michaelok Aug 02 '13 at 17:17