Android Question Converting decimal time to 24hr format

andyp

Member
Licensed User
Hi

I have times in decimal format, where 1.0 = 24hrs. My numbers are all between 0 and 1.0.

For example 0.837. I wish to display this in 24hr time format, so 20:05hrs. Excel does this conversion for you......

Multiplying 0.837 * 24 = 20.088 - not quite what I am looking for.......

Is there a function, or method to do this conversion?

Thank you!
 

udg

Expert
Licensed User
Longtime User
Hi, I don't know what Excel does, but maybe this could help:
B4X:
Dim mytime As Double = 0.837
Dim tsec As Double = 1/86400        'seconds on scale 0.0..1.0
Dim temp As Double = mytime / tsec   'mytime in seconds on scale
Dim mm1 As Int = temp /60            'mytime in minutes
Log(mm1)
Dim hr As Int = mm1 / 60            'hours
Log(hr)
Dim mm As Int = mm1 - (hr*60)       'minutes
Log(mm)
 
Upvote 0

andyp

Member
Licensed User
Hi Peter - Sorry, but that missed what I was after :) Decimal 1.0 = 24hrs.....

Udg - Thank you! Works great. Would you be able to show me how to display this as xx:yy? ie two digits for hours, two for minutes?

I can do this

B4X:
Log(hr & ":" & mm & "hrs")

But how do I force an Int to be two digits? So instead of say 4:5hrs, it would display 04:05hrs?

Thanks again!
 
Upvote 0

udg

Expert
Licensed User
Longtime User
B4X:
Log($"$2.0{hr}:$2.0{mm}"$)

That should do the trick.
 
Upvote 0

andyp

Member
Licensed User
Thank you! I was just reading about smart string literal :)

Really nice solution - much better than my backup plan of adding a zero if the number was <= 9

Thanks again!
 
Upvote 0

udg

Expert
Licensed User
Longtime User
IMHO, smart string literal is really a powerful tool; keep reading it and experiment a bit. It's well worth your time.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Is there a function, or method to do this conversion?
Hi, I don't know what Excel does, but maybe this could help:

Here is another alternative pushing the DateUtils:
B4X:
Dim mytime As Double = 0.837
    Dim m As Long = mytime*24*DateTime.TicksPerHour
    Dim p As Period = DateUtils.PeriodBetween(0,m)
    Log($"$2.0{p.Hours}:$2.0{p.Minutes}:$2.0{p.Seconds}"$)  'displays 20:05:16 You can get rid of seconds if you want
 
Upvote 0
Top