Results 1 to 8 of 8

Thread: [RESOLVED] Return HH:MM from Number of Seconds

  1. #1

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Resolved [RESOLVED] Return HH:MM from Number of Seconds

    Hello everybody,

    I have a function that returns HH:MM:SS from a number of seconds. I want the function to instead return HH:MM. I know I can just round up the minutes if seconds are more than zero, and then if the minutes become 00 I round up the hours. However I was wondering if there was a more elegant solution. Thank you.

    Here is my HH:MM:SS function

    Code:
    Private Function TimeBetweenSeconds(TimeBegin As Variant, TimeEnd As Variant) As String
    
    Dim intTimeOne As Double
    Dim intTimeTwo As Double
    Dim intDifference As Double
    
    On Error GoTo ErrorHandler
    
    intTimeOne = hour(TimeBegin) * 60 + minute(TimeBegin) + second(TimeBegin) / 60
    intTimeTwo = hour(TimeEnd) * 60 + minute(TimeEnd) + second(TimeEnd) / 60
    
    intDifference = intTimeTwo - intTimeOne
    
    If intDifference < 0 Then
        intDifference = 1440 + intDifference
    End If
    
    TimeBetweenSeconds = Format$(CStr(Int(intDifference / 60)) & ":" & CStr(Int(((intDifference / 60) - Int(intDifference / 60)) * 60)) & ":" & round((intDifference - Int(intDifference)) * 60), "H:MM:SS")
    
    Exit Function
    ErrorHandler:
    TimeBetweenSeconds = ""
    
    End Function

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,743

    Re: Return HH:MM from Number of Seconds

    Do you really want rounding?
    Normally, for example when using the internal date/time functions, the seconds are stripped.

    If you have the number of seconds you can divide them by 86400 to get the internal date/time
    Code:
    Debug.Print Format(43200/86400,"HH:MM:SS")
    Debug.Print Format(43220/86400,"HH:MM:SS")
    
    Debug.Print Format(43200/86400,"HH:MM")
    Debug.Print Format(43220/86400,"HH:MM")

  3. #3

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Return HH:MM from Number of Seconds

    Quote Originally Posted by Arnoutdv View Post
    Do you really want rounding?
    Normally, for example when using the internal date/time functions, the seconds are stripped.

    If you have the number of seconds you can divide them by 86400 to get the internal date/time
    Code:
    Debug.Print Format(43200/86400,"HH:MM:SS")
    Debug.Print Format(43220/86400,"HH:MM:SS")
    
    Debug.Print Format(43200/86400,"HH:MM")
    Debug.Print Format(43220/86400,"HH:MM")
    Thanks for the reply. I have a countdown timer. Basically

    If the time remaining is 4:33:22 I want to display 4:34
    If the time remaining is 4:33:00 I want to display 4:33
    if the time remaining is 4:32:54 I want to display 4:33
    etc...

    Thanks.

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,743

    Re: Return HH:MM from Number of Seconds

    That's odd :-)

    Then just add 60 to the value if value / 60 gives a remainder <> 0

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,914

    Re: Return HH:MM from Number of Seconds

    Why not add 30, and then just strip off the printing of seconds. That would seem to "round" to minutes, although I agree: We usually truncate in these situations.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Return HH:MM from Number of Seconds

    Given seconds, I would usually just add 59 and then do a Mod 60. This will round everything from 1 to 59 seconds up to the next minute, but not round up 0 seconds so should do what you want without having to do a divide by 60 and check for a remainder and add 60. Not much different than what Arnoutdv said, but removes the If condition so is a one liner.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Return HH:MM from Number of Seconds

    Quote Originally Posted by Elroy View Post
    Why not add 30, and then just strip off the printing of seconds. That would seem to "round" to minutes, although I agree: We usually truncate in these situations.
    For a countdown timer rounding down looks odd when you have 29 seconds or more remaining, so you would be showing 00:00 hours and minutes left for around 30 seconds. It looks a bit better if you show you have 00:01 left and only show 00:00 when it actually expires.

  8. #8

    Thread Starter
    Hyperactive Member Hassan Basri's Avatar
    Join Date
    Sep 2006
    Posts
    324

    Re: Return HH:MM from Number of Seconds

    Quote Originally Posted by passel View Post
    Given seconds, I would usually just add 59 and then do a Mod 60. This will round everything from 1 to 59 seconds up to the next minute, but not round up 0 seconds so should do what you want without having to do a divide by 60 and check for a remainder and add 60. Not much different than what Arnoutdv said, but removes the If condition so is a one liner.
    Thanks passel. This is the solution I took which works great.

    Thanks also to Arnoutdv and Elroy for your contributions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width