Android Question I need seconds to days conversion

Scantech

Well-Known Member
Licensed User
Longtime User
B4X:
Sub ConvertMillisecondsToString(t As Long) As String
   Dim hours, minutes, seconds As Int
   hours = t / DateTime.TicksPerHour
   minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
   seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
   Return $"$1.0{hours}:$2.0{minutes}:$2.0{seconds}"$
End Sub

I see we have millisecond to hours, anyone knows how to code seconds to days? Thanks
 

emexes

Expert
Licensed User
I see we have millisecond to hours, anyone knows how to code seconds to days?
Divide by 86400 (= 24 hours x 60 minutes x 60 seconds). Division usually casts Int calculations to Double, but never hurts to check (which I did, and it does)

As a function:
B4X:
Sub ConvertSecondsToDays(NumSeconds As Long) As Double

    '''Return NumSeconds / 60 / 60 / 24    'long but self-explanatory way
    Return NumSeconds / 86400

End Sub
 
Upvote 0

Scantech

Well-Known Member
Licensed User
Longtime User
Well i did a milliseconds to days. I needed the other info (hours, min, and sec) as well.

B4X:
Sub ConvertMillisecondsToString(t As Long) As String
    Dim days, hours, minutes, seconds As Long
    days = t / DateTime.TicksPerDay
    hours = (t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    
    Return $"$1.0{days}:$2.0{hours}:$2.0{minutes}:$2.0{seconds}"$
        
End Sub
 
Upvote 0

emexes

Expert
Licensed User
Tada! :)
B4X:
Sub ConvertSecondsToComponents(S As Long) As Int()    'days, hours, minutes, seconds

    Dim NumDays As Int = S / 86400
    S = S - NumDays * 86400

    Dim NumHours As Int = S / 3600
    S = S - NumHours * 3600

    Dim NumMinutes As Int = S / 60
    S = S - NumMinutes * 60

    Dim NumSeconds As Int = S

    Return Array As Int(NumDays, NumHours, NumMinutes, NumSeconds)
   
End Sub

Sub ConvertSecondsToString(S As Long) As String    'D:HH:MM:SS

    Dim TC() As Int = ConvertSecondsToComponents(S)
 
    Return $"$1.0{TC(0)}:$2.0{TC(1)}:$2.0{TC(2)}:$2.0{TC(3)}"$

End Sub
Not tested, no need, I don't make no mistakes ;-)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The discussion above is not 100% accurate and it cannot be as more information about the requirements is needed.

The definition of "day" is more fluid than the definition of hour. There are days with 23, 24 and 25 hours. In some cases you can ignore it and in some cases you can't. For example if you want to calculate the seconds left till 17:00 on the next day.

You can use DateUtils.DateBetweenInDays to find the number of days between two time instances.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
In some cases you can ignore it and in some cases you can't.
Extremely true.

You can use DateUtils.DateBetweenInDays to find the number of days between two time instances.
I haven't verified it myself, but I would bet that this is a solid, high-quality way of doing it.


For those that find things like this interesting, I just wanted to add a relevant article, which might induce a headache, and also help motivate to use DateUtils instead of inventing something of your own:

Falsehoods programmers believe about time
https://gist.github.com/timvisee/fcda9bbdff88d45cc9061606b4b923ca
 
Upvote 0
Top