Formula for time display?

syncmaster13

Member
Licensed User
Longtime User
Hi
First, I’m glad to join basic4android community. Hopefully my programming skills will improve with time:sign0104: but for now I don’t even know what to look for in case I have a problem. And I already have 2 of them. I have a feeling that they are somehow related. Can somebody tell me how to solve it ?

1. I need to convert a number, let say 1461 (which is” in seconds”)to time format “hours:minutes:seconds”. And properly display it as text in label view.
2. How to display numbers like: 125.876543 to see 125 (without decimal point and without rounding, I tried NumberFormat2 but it doesn’t work )
Thank You
 

Mahares

Expert
Licensed User
Longtime User
Hre are the answers to your 2 questions. Welcome to the forum:
B4X:
Dim  TimeDiff As Long= 1461
      TimeDiff=1461*1000   'in milliseconds
      Dim  Hours, Minutes, Seconds As Int
      Hours = Floor(TimeDiff / DateTime.TicksPerHour)   
      TimeDiff = (TimeDiff Mod DateTime.TicksPerHour)
      Minutes = Floor(TimeDiff / DateTime.TicksPerMinute)
      TimeDiff = (TimeDiff Mod DateTime.TicksPerMinute)
      Seconds = Floor(TimeDiff / DateTime.TicksPerSecond)
      Msgbox(  Hours & " hrs " & Minutes & " mn " & Seconds & " sec" ,"") 'displays: 0 hrs 24 mn 21 sec
      
      Msgbox(Floor(125.6776645),"")  'displays 125
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
B4X:
Dim sec As Int = 1461
lblMyLabel.Text = DateTime.Time((sec - DateTime.TimeZoneOffset * 3600) * 1000)

B4X:
Floor(125.876543)

Opps. Sorry Mahares. I didn't see that you already answered this question.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Be careful:
B4X:
Label1.Text = Floor(125.876543)
displays 125.0 bacause Floor returns a Double and an ineger value in Double displays .0 at the end.

It should be:
B4X:
Label1.Text = NumberFormat2(Floor(125.876543), 1, 0, 0, False)
or
B4X:
Dim NbInt As Int
NbInt = Floor(125.876543)
Label1.Text = NbInt
Best regards.
 
Upvote 0

syncmaster13

Member
Licensed User
Longtime User
Now it works, Thanks again. Now I have to figure it out why “TimeDiff=” is repeated twice and what “Mod” does .

“TimeDiff = (TimeDiff Mod DateTime.........)”.

I used this example to make sure “125” is displayed without decimal point:

Dim NbInt As Int
NbInt = Floor(125.876543)
Label1.Text = NbInt
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Here are the details of the calculations:
For example: 1461000 mod 60000 means the remainder when you divide the first by the second. In this case the answer is:21000 . When you divide the first by the second you get 24 but we want the remainder which is 21000.
1461000 mod 3600000 gives 1461000. When you divide the first by the second you get 0 but the remainder is the entire first number.
A simple example: 17 mod 7 gives 3

B4X:
 Hours = Floor(TimeDiff / DateTime.TicksPerHour)      ' = 0
 TimeDiff = (TimeDiff Mod DateTime.TicksPerHour)  '= 1461000 mod  3600000 =1461000 msec
 Minutes = Floor(TimeDiff / DateTime.TicksPerMinute)    '=1461000/60000 = 24
TimeDiff = (TimeDiff Mod DateTime.TicksPerMinute)   '1461000 mod 60000 =  21000 msec
Seconds = Floor(TimeDiff / DateTime.TicksPerSecond)     '21000/1000 =21  sec
 
Upvote 0
Top