Android Question I need seconds to days conversion

Discussion in 'Android Questions' started by Scantech, Jul 30, 2019.

  1. Scantech

    Scantech Well-Known Member Licensed User

    Code:
    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
     
  2. emexes

    emexes Well-Known Member Licensed User

    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:
    Code:
    Sub ConvertSecondsToDays(NumSeconds As Long) As Double

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

    End Sub
     
    mendiburen likes this.
  3. Scantech

    Scantech Well-Known Member Licensed User

    Well i did a milliseconds to days. I needed the other info (hours, min, and sec) as well.

    Code:
    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
     
  4. emexes

    emexes Well-Known Member Licensed User

    Tada! :)
    Code:
    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: Jul 30, 2019
  5. Erel

    Erel Administrator Staff Member Licensed 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.
     
    mendiburen likes this.
  6. Sandman

    Sandman Well-Known Member Licensed User

    Extremely true.

    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
     
    Claudio Oliveira, fredo and Erel like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice