Android Question String to Time

NewDD

Member
Hi, I have a string that goes like this:

16:35:00

I need to convert it to be like this:

4:35

(Convert from 24h to 12h and remove the seconds)

Can you help me please?
 

emexes

Expert
Licensed User
Longtime User
(Convert from 24h to 12h and remove the seconds)

Or more directly, and including am/pm indicator to make 12am (midnight) distinct from 12pm (noon midday) :

B4X:
Dim TestTime() As String = Array As String( _
    "00:17:23", "01:23:11", "07:07:94", "11:59:13", "12:00:00", "12:59:56", _
    "13:00:00", "13:14:15", "16:35:00", "23:33:43", "24:23:22", _
    "12:34", "no:no:no", "", "hello" _
)
  
For I = 0 To TestTime.Length - 1
    Log(TestTime(I) & " ==> " & ReformatTime(TestTime(I)))
Next

Sub ReformatTime(T24 As String) As String
  
    Dim T() As String = Regex.Split(":", T24)
  
    If T.Length = 3 Then
        If IsNumber(T(0)) And IsNumber(T(1)) Then
            Dim Hours As Int = T(0) Mod 24
          
            'do this before adjusting Hours number
            Dim AMPM As String = "am"
            If Hours >= 12 Then
                AMPM = "pm"
            End If
          
            If Hours > 12 Then
                Hours = Hours - 12
            else if Hours = 0 Then
                Hours = 12    '00 in 24h time = 12am in 12h time
            End If
  
            Return Hours & ":" & T(1) & AMPM
        End If
    End If

    Return "expecting ""hh:mm:ss"" not """ & T24 & """"    'or some other indication that something's not right

End Sub

Log output:
Waiting for debugger to connect...
Program started.
00:17:23 ==> 12:17am
01:23:11 ==> 1:23am
07:07:94 ==> 7:07am
11:59:13 ==> 11:59am
12:00:00 ==> 12:00pm
12:59:56 ==> 12:59pm
13:00:00 ==> 1:00pm
13:14:15 ==> 1:14pm
16:35:00 ==> 4:35pm
23:33:43 ==> 11:33pm
24:23:22 ==> 12:23am
12:34 ==> expecting "hh:mm:ss" not "12:34"
no:no:no ==> expecting "hh:mm:ss" not "no:no:no"
 ==> expecting "hh:mm:ss" not ""
hello ==> expecting "hh:mm:ss" not "hello"
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Another solution:
B4X:
DateTime.TimeFormat="HH:mm:ss"
    Log(DateTime.Time(DateTime.Now))  '16:35:00
    DateTime.TimeFormat="hh:mm a"  'if you do not want the am/pm make it hh:mm
    Log(DateTime.Time(DateTime.Now))  '04:35 AM
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
Another solution:
B4X:
DateTime.TimeFormat="HH:mm:ss"
    Log(DateTime.Time(DateTime.Now))  '16:35:00
    DateTime.TimeFormat="hh:mm a"  'if you do not want the am/pm make it hh:mm
    Log(DateTime.Time(DateTime.Now))  '04:35 AM

That is certainly a nicer solution than my character-twiddling, but I've always been a bit wary of DateTime feeling like a global variable, with changes possibly having ramifications elsewhere in the program.

Like, on the one hand we have: Each thread has its own instance of DateTime.

and on the other hand we have: Set the format right before you call DateTime.DateParse. Many things can happen while the sub is waiting for something.

My wariness isn't helped by the often-unexpected results brought about when DateTime calculations cross daylight saving adjustments. Eg if you work 11pm to 7am, then that's obviously an 8 hour shift, right? ?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I've always been a bit wary of DateTime
The OP did not indicate under what conditions he wants the changes and when they are warranted. That is why with your solution, jahswant, others and the bit I posted he has to make his choice as to what fits his scenario best.
 
Upvote 0

NewDD

Member
Thank you all, I wrote this post two days ago but it has just been approved! :rolleyes:
Well, a lot of things has happened since Friday; But any way, thank you for the answers.
And a word for the mods; please approve our posts in time. Thanks.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I wrote this post two days ago but it has just been approved!
There is no approval wait time in this forum. You post your thread and any member can respond to it when they think they can help. It can be an immediate response or it may take a few hours or days to respond, depending on the question.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
And a word for the mods; please approve our posts in time. Thanks.
Consider a small donation to have your accont upgraded to the "Licensed User" status so that your posts will be published without delay and moderation.
 
Upvote 0

NewDD

Member
Consider a small donation to have your accont upgraded to the "Licensed User" status so that your posts will be published without delay and moderation.
Oh, I see, I will be glad to do so in the next chance I have. Thank you for pointing this out.
 
  • Like
Reactions: udg
Upvote 0
Top