# Android Questionproblem with a time calculation

#### FrankDev

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

#### Magma

##### Expert
Longtime User
I think you need to split hours and minutes... using datetime and ticks you can convert to anything you want but not have in mind hh:mm is wrong because the limit 23:59..

#### FrankDev

##### Active Member
Longtime User
I get the data (HH:MM) delivered like this and have to 'forward' them like this (HH:MM)

#### Erel

##### B4X founder
Staff member
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``````

#### Magma

##### Expert
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

#### Magma

##### Expert
Longtime User
how do i convert the 60.5 hours to ms then?
Sorry...

60 hours .5 hour

#### Magma

##### Expert
Longtime User
Split the 60 with .5...

The minutes... * min per hour * secs * 1000ms
.5 * 60 * 60 * 1000 = ms

And calc the hours

Sum will give total ms

I m from phone sorry for errors

#### BlueVision

##### Active Member
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:

#### Erel

##### B4X founder
Staff member
Longtime User
Calculating with ticks will work. Converting time strings with DateTime.Time or the parse methods will not work (in some cases) as those strings represent a specific time instance and not duration.

#### FrankDev

##### Active Member
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

#### BlueVision

##### Active Member
Longtime User
Thanks for clarification Erel!

#### Magma

##### Expert
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 ! :~ใ

#### FrankDev

##### Active Member
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``````

#### Erel

##### B4X founder
Staff member
Longtime User
B4X:
``````Sub ConvertMillisecondsToString(t As Long) As String
Dim sign As String = IIf(t < 0, "-", "")
t = Abs(t)
Dim hours, minutes As Int
hours = t / DateTime.TicksPerHour
minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
Return \$"\${sign}\$1.0{hours}:\$2.0{minutes}"\$
End Sub``````

#### FrankDev

##### Active Member
Longtime User
thank you very much...

works

Replies
8
Views
475
Android Question Add Hours to DateTime
Replies
8
Views
3K
Replies
22
Views
17K
Replies
105
Views
97K