Android Question Time formatting problem

Discussion in 'Android Questions' started by Jack Cole, Jul 28, 2019.

  1. Jack Cole

    Jack Cole Active Member Licensed User

    I'm trying to convert a 24 hour time to 12 hour with AM/PM. I've tried to use the documentation for date/time formats.

    Here's my code:
    Code:
    Dim TimeString as String = "21:00"
    DateTime.DateFormat="HH:mm"
    Dim TimeValue=DateTime.TimeParse(TimeString&":00"as Long
    DateTime.DateFormat= "h:mm a"
    Log(DateTime.Time(TimeValue))
    The first problem is that TimeParse won't parse a time with the format "HH:mm" for the time of "21:00." I have to add :00 on the end of the string to make that work. The next problem happens when I try to get the time formatted to "h:mm a", which according to documentation should yield "9:00 PM". What I actually get is "21:00:00".

    I can of course write code to do all this manually, but I am puzzled about why this won't work.
     
  2. mw71

    mw71 Active Member Licensed User

    DateTime.DateFormat= "h:mm a" -> you mean TIMEFormat ??
     
    Jack Cole and emexes like this.
  3. Jack Cole

    Jack Cole Active Member Licensed User

    Haha. Yes, I figured I was missing something obvious. That was the problem. Thank you.

    Here is my final code in case it would be useful to anyone else.

    Code:
    Sub ConvertTo12HrTime(TimeToConvert As String, WithLeadingZero As Boolean) As String
        
    Dim OldTimeFormat As String
        OldTimeFormat=
    DateTime.TimeFormat
        
    DateTime.TimeFormat="HH:mm"
        
    Dim TimeVal=DateTime.TimeParse(TimeToConvert) As Long
        
    If WithLeadingZero Then
            
    DateTime.TimeFormat="hh:mm a"
        
    Else
            
    DateTime.TimeFormat="h:mm a"
        
    End If   
        
    Dim TimeReturnVal=DateTime.Time(TimeVal) As String
        
    DateTime.TimeFormat=OldTimeFormat
        
    Return TimeReturnVal
    End Sub

    Sub ConvertTo24HrTime(TimeToConvert As String, WithLeadingZero As Boolean) As String
        
    Dim OldTimeFormat As String
        OldTimeFormat=
    DateTime.TimeFormat
        
    DateTime.TimeFormat="h:mm a"
        
    Dim TimeVal=DateTime.TimeParse(TimeToConvert) As Long
        
    If WithLeadingZero Then
            
    DateTime.TimeFormat="HH:mm"
        
    Else
            
    DateTime.TimeFormat="H:mm"
        
    End If
        
    Dim TimeReturnVal=DateTime.Time(TimeVal) As String
        
    DateTime.TimeFormat=OldTimeFormat
        
    Return TimeReturnVal
    End Sub
     
    emexes likes this.
  4. emexes

    emexes Well-Known Member Licensed User

    +liked, although I'm looking at those two routines being so similar that you could merge them into being a single more general routine, and then have your two specific (and reasonably so, since they are probably the most likely) conversions become one-to-five-liners.
     
    Jack Cole and Shelby like this.
  5. emexes

    emexes Well-Known Member Licensed User

    btw the bit that tipped me to +like was that you save and restore the time format :)

    (just in case you thought that nicety went unnoticed)
     
    Jack Cole and Shelby like this.
  6. Jack Cole

    Jack Cole Active Member Licensed User

    Yes, I think so too. Probably could just specify the direction of the conversion as a parameter of the sub. But there is some clarity added by having them separate.
     
  7. emexes

    emexes Well-Known Member Licensed User

    I was thinking more of including the formats as parameters, eg:

    Sub TimeReformat(TheTime As String, OldFormat As String, NewFormat As String) As String

    and in action it would look like:

    Log( TimeReformat("21:00", "HH:mm", "h:mm a") )

    Great minds think alike :)
     
    Jack Cole likes this.
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