Results 1 to 7 of 7

Thread: better way to convert seconds into dd:hh:mm:ss?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210

    better way to convert seconds into dd:hh:mm:ss?

    right now i have
    Code:
    'Start time conversion
            If Secs > 60 Then
                Do
                    Secs = Secs - 60
                    Mins = Mins + 1
                Loop Until Secs < 60
            End If
            If Mins > 60 Then
                Do
                    Mins = Mins - 60
                    Hours = Hours + 1
                Loop Until Mins < 60
            End If
            If Hours > 24 Then
                Do
                    Hours = Hours - 24
                    Days = Days + 1
                Loop Until Hours < 24
            End If
    and i don't think it's really efficient since this will be calculated every second.

    in Secs, i have a huge number, which is the difference in time between today and some time after. i'd like to convert that into "dd Days hh Hours mm Minutes ss Seconds" without being as inefficient as what i have above. i could just format it as "dd:hh:mm:ss" but i'd like to have "Days Hours Minutes, and Seconds" though.
    [vbcode]
    Me.Hide()
    [/vbcode]

  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    VB Code:
    1. Try
    2.  
    3.             Dim seconds As Integer
    4.             Dim d As Integer
    5.             Dim h As Integer
    6.             Dim m As Integer
    7.             Dim s As Integer
    8.  
    9.             seconds = 86401 'one day, zero hours and mins and one sec
    10.  
    11.  
    12.             If (seconds >= 86400) Then
    13.                 d = seconds \ 86400
    14.                 seconds = seconds Mod 86400
    15.             End If
    16.  
    17.             If (seconds >= 3600) Then
    18.                 h = seconds \ 3600
    19.                 seconds = seconds Mod 3600
    20.             End If
    21.  
    22.             If (seconds >= 60) Then
    23.                 m = seconds \ 60
    24.                 seconds = seconds Mod 60
    25.             End If
    26.             s = seconds
    27.                 Console.WriteLine("days " & "hours " & "mins " & "secs")
    28.         Console.WriteLine(d & " " & h & " " & m & " " & s)
    29. Catch ex As Exception
    30.         End Try
    is that what you need?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    hmm.. almost. thanks.
    i read about the operater \ but forgot. what does that do?

    EDIT:

    i was confused. that was _exactly_ what i needed.
    still, i don't know what \ does...
    [vbcode]
    Me.Hide()
    [/vbcode]

  4. #4
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    This is from MSDN



    \ operator

    Divides two numbers and returns an integer result.

    number1 \ number2
    Parts
    number1
    Required. Any numeric expression of an integral type.
    number2
    Required. Any numeric expression of an integral type.
    Supported Types
    Byte, Short, Integer, or Long.

    Result
    The result is the integer quotient of number1 and number2, dropping the remainder.

    Remarks
    Before division is performed, any floating-point numeric expressions are coerced to Long if Option Strict is Off. If Option Strict is On, a compiler error results.

    The data type of the result is Byte, Short, Integer, or Long. Any fractional portion is truncated.

    If any expression is stated as Nothing, it is treated as zero. Attempting to perform integer division by zero causes a DivideByZeroExeception to be thrown.

    Example
    This example uses the \ operator to perform integer division. The result is an integer representing the integer quotient of the two operands.

    Dim myValue As Integer
    myValue = 11 \ 4 ' Returns 2.
    myValue = 9 \ 3 ' Returns 3.
    myValue = 100 \ 3 ' Returns 33.
    MyValue = 67 \ -3 ' Returns -22.


    but, the short answer is: Divides two numbers and returns an integer result.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    VB Code:
    1. MsgBox(TimeSpan.FromSeconds(6000).ToString)

    Days will only be shown if there are any.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2002
    Posts
    210
    lol... that makes me laugh.
    so simple. yet so difficult when you don't know about it.
    final version:
    VB Code:
    1. Dim Seconds As Integer = DateDiff(DateInterval.Second, Now, dtpDue.Value)
    2.         Dim d As Integer = TimeSpan.FromSeconds(Seconds).Days
    3.         Dim h As Integer = TimeSpan.FromSeconds(Seconds).Hours
    4.         Dim m As Integer = TimeSpan.FromSeconds(Seconds).Minutes
    5.         Dim s As Integer = TimeSpan.FromSeconds(Seconds).Seconds
    6.         MsgBox(d & " Days " & h & " Hours " & m & " Minutes " & s & " Seconds")
    thanks again, guys.
    [vbcode]
    Me.Hide()
    [/vbcode]

  7. #7
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    awesome. I didn't knw about that either!!

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