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

Rick Harris

Well-Known Member
Licensed User
Longtime User
Is there a simple way to determine the timezone offset for any place on Earth, regardless of the Android's device timezone setting? For example I live in Amsterdam (my device is set to GMT+1) and I want to know the time in for example Hong Kong. This implies that somehow it can be determined whether a distinction between summer and wintertime exists for any place on earth, plus on which dates the switch takes place.
 
Last edited:
Upvote 0
Top