Android Question Date/Time to UTC

ValDog

Active Member
Licensed User
I am looking to convert a date/time string, formatted say as "03/07/2015 11:11:44" to UTC format. Can anyone point me in the right direction?
 

ValDog

Active Member
Licensed User
Thanks for responding. Pretty sure I've read that a number of times, and have seen only one unrelated reference to UTC. I'm looking for a bit more direction...
 

NJDude

Expert
Licensed User
Well, if you read the documentation above, one way of doing it would be setting the time zone to 0, that will give you the UTC time.
 

ValDog

Active Member
Licensed User
What exactly do you mean with UTC format? What is the time zone of the date string you wrote?
Time zone I currently wrote in is USA - Eastern, but my application could be in use anywhere in the world - so I need to output the local date/time in UTC.
 
Last edited:

ac9ts

Active Member
Licensed User
This will give you the GMT time for any local time.

B4X:
lblUTCTime.Text = DateTime.Time(DateTime.Now - DateTime.TicksPerHour * TZOffset)
B4X:
Public Sub GetTimeZoneOffset As Int
   
    Dim s, d As String
    Dim l As Long
   
    s = DateTime.DateFormat
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
   
    l = DateTime.Now
    d = DateTime.Date(l) & " GMT"
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss z"
   
    Dim res As Int
    res = -Round((l - DateTime.DateParse(d))/3600000)
    DateTime.DateFormat = s
   
    Return res
   
End Sub
 

ValDog

Active Member
Licensed User
This will give you the GMT time for any local time.

B4X:
lblUTCTime.Text = DateTime.Time(DateTime.Now - DateTime.TicksPerHour * TZOffset)
B4X:
Public Sub GetTimeZoneOffset As Int
  
    Dim s, d As String
    Dim l As Long
  
    s = DateTime.DateFormat
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss"
  
    l = DateTime.Now
    d = DateTime.Date(l) & " GMT"
    DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss z"
  
    Dim res As Int
    res = -Round((l - DateTime.DateParse(d))/3600000)
    DateTime.DateFormat = s
  
    Return res
  
End Sub
Thanks - this is good. I'll be able to use this for real time measurements.

How could I go about converting existing date/time strings (formatted as "03/07/2015 11:11:44") to UTC - given that I know the timezone they were collected in.
 

ac9ts

Active Member
Licensed User
TZoffset is the number of hours from GMT/UTC you are currently based on the timezone setting. Just do some math with the offset.
 

Erel

Administrator
Staff member
Licensed User
There are several possible ways.

This code adds the EST timezone to the original string and the prints the UTC time:
B4X:
DateTime.SetTimeZone(0) 'only need to set it once so the process is now based on UTC timezone.
DateTime.DateFormat = "MM/dd/yyyy HH:mm:ss z"
Dim ticks As Long = DateTime.DateParse("03/07/2015 11:11:44" & " EST")
Log(DateTime.Date(ticks))
 

Troberg

Well-Known Member
Licensed User
This will give you the GMT time for any local time.

B4X:
lblUTCTime.Text = DateTime.Time(DateTime.Now - DateTime.TicksPerHour * TZOffset)
B4X:
(snip)
    res = -Round((l - DateTime.DateParse(d))/3600000)
(snip)
Just curious, is there any reason you use DateTime.TicksPerHour in one case and 3600000 in the other? Some hidden trap I can't see?
 

ac9ts

Active Member
Licensed User
I copied the code with 3600000 from somewhere and wrote the code with DateTime.TicksPerHour. The copied code is in a utilities module that I hardly ever edit (kind of a "Black-Box" chunk of code.
 
Top