B4J Question [ABMaterial] Get Browsers TimeZone Offset

Harris

Expert
Licensed User
Longtime User
Using tz = DateTime.GetTimeZoneOffsetAt(DateTime.Now) gives me the servers offset (which is 0 - UTC - which is correct).

How to get the connected browsers offset (again - seems I asked this already - stupid brain)?

Searched my code and the forum but can't find...

Thanks
 

alwaysbusy

Expert
Licensed User
Longtime User
You do have ABM.Util.GetBrowserTimeZone(page) which reternes a timezone name.

if you want to have the offset, you could try this (untested):
B4X:
Dim script As String = $"var d = new Date();
return d.getTimezoneOffset();"$

Dim tz As Future = page.ws.EvalWithResult(script, Null)
Log(tz.Value)
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
It works by returning the number of minutes offset. Simply divide by 60 to get the hours.
Since we are negative in North America, times by -1 to get the final result (-7) for me right now.
Hopefully this will compensate for DST - we shall see.

ABM.Util.GetBrowserTimeZone(page) ' returns America/Vancouver. This would be good if I had the timezone tables setup in MySQL - which I don't.


Thanks for this.

B4X:
Sub GetUsertz() As Double
   
    Dim SQL As SQL = DBM.GetSQL
    Dim tzo As List = DBM.SQLSelect(SQL,"Select * FROM users WHERE UserID = "&UserID)
    Dim tz As Double =  0   
   
    If tzo.Size > 0 Then
        Dim tbl As Map = tzo.Get(0)
       
        tz  = tbl.Get("usertz")
        Dim dname As String = tbl.Get("username")
        Username = dname       
       
    End If
   
    DBM.CloseSQL(SQL)


    If tz = 0 Then ' if user timezone was not set - set it to browser tz...

        Dim script As String = $"var d = new Date();
        return d.getTimezoneOffset();"$

        Dim tzf As Future = page.ws.EvalWithResult(script, Null)
        Log(" tzf: "& (tzf.Value / 60) )   
        tz = (tzf.Value / 60) * -1  ' (420 / 60 * -1 = -7)

    End If
    Dim tzn As String = ABM.Util.GetBrowserTimeZone(page) ' returns America/Vancouver
   
    DateTime.SetTimeZone( tz)
    Main.utz = tz
    Log(" Set time zone to: "&tz&"  Name: "&tzn)
   
    Return tz
   
End Sub
 
Upvote 0
Top