Rajnikant Patel
Rajnikant Patel

Reputation: 571

Getting Client's Timezone in GWT

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?

Upvotes: 3

Views: 8144

Answers (3)

Yeser Amer
Yeser Amer

Reputation: 315

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.

Upvotes: 2

user2508244
user2508244

Reputation: 91

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.

Upvotes: 0

Jason Washo
Jason Washo

Reputation: 615

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().

Upvotes: 1

Related Questions