Android Question [SOLVED] Return a leading zero if needed using DateTime.GetHour

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Using the code below, is there a way to have GetHour and GetMinute display a "0" in front of the string if the returned value is less than 10? If the converted hours = "9" for 9 in the morning, I would like the returned string to have "09" in it.

B4X:
    Dim strMyTimeString As String = _
        DateTime.GetHour(lngMyTics) & ":" & DateTime.GetMinute(lngMyTics)

Thanks
 

udg

Expert
Licensed User
Longtime User
"DateTime.GetHour" returns an INT; you could use NumberFormat to add a leading zero.
Something like:
NumberFormat(DateTime.GetHour(lngMyTics), 2 ,0)
Or, even better, smart string literals:
B4X:
Dim strMyTimeString As String =  _
$"$2{DateTime.GetHour(lngMyTics)}:$2{DateTime.GetMinute(lngMyTics)}"$
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Or you could have just used at the beginning of your code.

B4X:
DateTime.TimeFormat = "hh:mm"

Or am I missing something here.

Enjoy...
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Peter,

I will try that one as well. Looks like that one has more flexibility.

Cheers.
 
Upvote 0
Top