Android Question Date/Time to UTC

ValDog

Active Member
Licensed User
Longtime 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
Longtime 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...
 
Upvote 0

ValDog

Active Member
Licensed User
Longtime 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:
Upvote 0

ac9ts

Active Member
Licensed User
Longtime 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
 
Upvote 0

ValDog

Active Member
Licensed User
Longtime 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.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime 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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime 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))
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime 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?
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime 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.
 
Upvote 0
Top