Android Question problem with a time calculation

FrankDev

Active Member
Licensed User
Longtime User
Hello

I have a problem with a time calculation.

A user can borrow a product for a certain time.
I have this time as double

e.g.
60.5 hours (means 60 hours / 30 minutes)

the actual usage time I have in the format 10:30 (so 10 hours, 30 minutes / string variable)

now I need the remaining amount as HH:mm (String)

so the calculation looks like this:

60.5 (hours) - 10:30 = 50:00

so I need how I convert the hours or the hour:minute so,
that as result

hh:mm (as string)

comes out

Best regards
Frank
 

BlueVision

Active Member
Licensed User
Longtime User
Another way of thinking:
You probably want to calculate the remaining time. So why not use the clicks? You have the start time and a defined time until the end. So the start time must be recorded and the end time (start time + running time in clicks). Both are click values that can be quickly analysed and converted using Date.Time functions without having to experiment with different string formats.

I use this principle to give apps a limited lifetime. If the end time is exceeded, the app closes immediately after starting for instance.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: don't use DateTime methods for this. Explanation in the video tutorial about date & time.

B4X:
Sub AppStart (Args() As String)
    Dim d1 As Long = 60.5 * DateTime.TicksPerHour
    Dim d2 As Long = ConvertHHMMToMilliseconds("10:30")
    Dim d3 As Long = d1 - d2
    Log(ConvertMillisecondsToString(d3))
End Sub

Sub ConvertMillisecondsToString(t As Long) As String
    Dim hours, minutes As Int
    hours = t / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    Return $"$1.0{hours}:$2.0{minutes}"$
End Sub

Sub ConvertHHMMToMilliseconds (s As String) As Long
    Dim m As Matcher = Regex.Matcher("(\d+):(\d+)", s)
    m.Find
    Dim hours As Int = m.Group(1)
    Dim minutes As Int = m.Group(2)
    Return hours * DateTime.TicksPerHour + minutes * DateTime.TicksPerMinute
End Sub
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
I get the data (HH:MM) delivered like this and have to 'forward' them like this (HH:MM)
So you have start hh:mm and end hh:mm.. the simplest is to split hh and mm .. do the calc and rejoin to string..

Unless you have a date
 
Upvote 0

BlueVision

Active Member
Licensed User
Longtime User
Tip: don't use DateTime methods for this.
Just for my understanding:

If you calculate with absolute (unique) tick values (date & time), it should work.
Of course, this does not work with "times" that are then somehow subsequently assigned to a string that was obtained from a tick value.
The basis must always be the tick value itself. Then you can also calculate a tick value in the future and the duration. If you put both values in relation to each other, it works wonderfully.

just to proove my thinking...
 
Last edited:
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
@Erel

I have another display error

if I calculate the whole e.g. in reverse.

so e.g. an employee works 40 hours a week
so far he has worked 30.5 hours

then I get as a display at

30.5 - 40

-9:-30
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Simple algebra and physics want the values to be at same measurement unit...

Also programming language need to specify what will be float.. int string...

As long you are using them right .. you will have the result you want..

I always doing wrong way ! :~》
 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
because of the display error (-9:-30)

is that possible

B4X:
Sub ConvertMillisecondsToString(t As Long) As String
    Dim hours, minutes As Int
    hours = t / DateTime.TicksPerHour
    minutes = Abs((t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute) '->  Abs ??
    Return $"$1.0{hours}:$2.0{minutes}"$
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…