Android Question How do I get my device's timezone and daylight savings?

Highwinder

Active Member
Licensed User
Longtime User
I have searched the forums, but have not come across what I'm looking for yet. I need to get the current time zone offset and daylight savings info of the user's device.

ie, if I'm in Seattle, I would need to get "-8" back as the time zone offset.

I also need to determine if daylight savings is in effect on the device or not.

How can I get this from the device?

Thanks!
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
DateTime.TimeZoneOffset returns the current time zone.

You can use this code to determine whether DST is currently in effect:
B4X:
Sub IsDaylightInEffect As Boolean
   Dim currentTimezone As Double = DateTime.TimeZoneOffset
   Dim t As Long = DateTime.Now
   For i = 0 To 11 Step 2
     If DateTime.GetTimeZoneOffsetAt(t) < currentTimezone Then Return True
     t = t + i * DateTime.TicksPerDay * 30
   Next
   Return False
End Sub
 
Upvote 0

Highwinder

Active Member
Licensed User
Longtime User
DateTime.TimeZoneOffset returns the current time zone.

You can use this code to determine whether DST is currently in effect:
B4X:
Sub IsDaylightInEffect As Boolean
   Dim currentTimezone As Double = DateTime.TimeZoneOffset
   Dim t As Long = DateTime.Now
   For i = 0 To 11 Step 2
     If DateTime.GetTimeZoneOffsetAt(t) < currentTimezone Then Return True
     t = t + i * DateTime.TicksPerDay * 30
   Next
   Return False
End Sub


This was very helpful and worked great, thank you so much!
 
Upvote 0
Top