Get browser timezone offset

Hi

I need to get the user’s local timezone offset via the browser in my Vaadin 12.0.0 application. I tried retrieving the WebBrowser object for the session but the timezoneOffset is always 0 and the timeZoneId is null. Is this a bug or am I going about this the wrong way?

Browser is Chrome, I can see that the offset is provided by Chrome when I run the following JavaScript code snippet:

var offset = new Date().getTimezoneOffset(); console.log(offset);

thanks
Jenny

Hi, it looks like a bug. VaadinSession.getCurrent().getBrowser().getTimezoneOffset() always returns 0 for me too. Can you please file a new issue? https://github.com/vaadin/flow/issues

Here’s a workaround:

@Route
public class MainView extends VerticalLayout {

    private int timeZoneOffset;

    public MainView() {
        UI.getCurrent().getPage().executeJavaScript(
                "$0.$server.setTimeZoneOffset(new Date().getTimezoneOffset());", getElement());

    }

    @ClientCallable
    public void setTimeZoneOffset(int timeZoneOffset) {
        this.timeZoneOffset = timeZoneOffset;
        initLayout();
    }

    private void initLayout() {
        Notification.show("TimeZoneOffset: " + timeZoneOffset);
    }

}

Thanks for that Alejandro, I logged a bug https://github.com/vaadin/flow/issues/4941.

I’ll use your workaround in the meantime.

thanks
Jenny

Hi !
Workaround not good enough for me because I use the getRawTimezoneOffset() vaadin 8 function

VaadinSession.getCurrent().getBrowser().getRawTimezoneOffset()

also, my call is not performed from a DIV element…

Seems that there’s still no fix in Vaadin 14 …