Android Question Date/Time to UTC

Discussion in 'Android Questions' started by ValDog, Mar 7, 2015.

  1. ValDog

    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?
     
  2. NJDude

    NJDude Expert Licensed User

    DonManfred likes this.
  3. ValDog

    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...
     
  4. NJDude

    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.
     
    DonManfred likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    What exactly do you mean with UTC format? What is the time zone of the date string you wrote?
     
  6. ValDog

    ValDog Active Member Licensed User

    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: Mar 8, 2015
  7. ac9ts

    ac9ts Active Member Licensed User

    This will give you the GMT time for any local time.

    Code:
    lblUTCTime.Text = DateTime.Time(DateTime.Now - DateTime.TicksPerHour * TZOffset)
    Code:
    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
     
  8. ValDog

    ValDog Active Member Licensed User

    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.
     
  9. ac9ts

    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.
     
  10. Erel

    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:
    Code:
    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))
     
  11. ValDog

    ValDog Active Member Licensed User

    Thank you all for your help!!!
     
  12. Troberg

    Troberg Well-Known Member Licensed User

    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?
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Avoid using such date / time arithmetics. Date handling is much more complicated than it may first seem.

    You should instead use DateTime and DateUtils API.
     
  14. ac9ts

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice