Android Question Time formatting problem

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:
B4X:
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.
 

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.

B4X:
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

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

Active 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.
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.
 

emexes

Well-Known Member
Licensed User
could just specify the direction of the conversion
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") )

But there is some clarity added by having them separate.
Great minds think alike :)
 
Top