Android Question Get the time in UTC

Najirm

Member
Licensed User
Longtime User
B4X:
DateTime.SetTimeZone(0)
DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"

Dim now As Long
now = DateTime.now
Log("The date is: " & DateTime.Date(now))

With the above code I'm getting the time in UTC. But somewhere I read like we shouldn't set SetTimeZone() to zero.

So which is the correct way to get the time in UTC.

Current time
B4X:
2015-10-06T13:29:32Z

Time in UTC
B4X:
2015-10-06T09:29:32Z
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Instead of setting the time zone, just get the time zone offset and add its negative to the current time.
 
Upvote 0

Najirm

Member
Licensed User
Longtime User
By setting DateTime.SetTimeZone(-(DateTime.GetTimeZoneOffsetAt(now))) the output is

B4X:
2015-10-06T05:29:32Z
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
This is what I meant:
B4X:
Sub UTCTime As String
    DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
    Dim offset As Double = DateTime.GetTimeZoneOffsetAt(DateTime.Now)
    Dim result As String = DateTime.Date(DateTime.Now-offset*DateTime.TicksPerHour)
    Log(result)
    Return result
End Sub
 
Upvote 0
Top