Convert SECONDS to HOURS and MINUTES in ASP vbscript

Author Message
Shawnm

  • Total Posts : 4
  • Scores: 0
  • Reward points : 0
  • Joined: 3/30/2009
  • Status: offline
Convert SECONDS to HOURS and MINUTES in ASP vbscript Wednesday, August 11, 2010 8:32 AM (permalink)
0
Hello.

I have a integer second value, for example 89493 seconds.

I would like to get two values from that:

1) Hours
2) Minutes

So ideally, it would return 3 separate values - hours, minutes, and seconds.

It would accept one input parameter - the SECONDS to calcuate.

I've tried all kinds of bulit in stuff, but none of it helps. I already am using the DATEDIFF function in SQL to get the seconds.

THank you for your help!
 
#1
    ebgreen

    • Total Posts : 8219
    • Scores: 98
    • Reward points : 0
    • Joined: 7/12/2005
    • Status: offline
    Re:Convert SECONDS to HOURS and MINUTES in ASP vbscript Thursday, August 12, 2010 1:59 AM (permalink)
    0
    nTotalSeconds = 89493
    nHours = CInt(nTotalSeconds/3600)
    nTotalSeconds = nTotalSeconds - (3600 * nHours)
    nMinutes = CInt(nTotalSeconds/60)
    nSeconds = nTotalSeconds - (60 * nMinutes)

    Now you have three variables that hold the data you want. nHours, nMinutes, and nSeconds.
    "... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
    Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
    http://www.visualbasicscript.com/m_47117/tm.htm
     
    #2
      Shawnm

      • Total Posts : 4
      • Scores: 0
      • Reward points : 0
      • Joined: 3/30/2009
      • Status: offline
      Re:Convert SECONDS to HOURS and MINUTES in ASP vbscript Thursday, August 12, 2010 5:53 AM (permalink)
      0
      Hi - Thanks so much for your reply!

      I plugged in the code and it is pretty much what I am looking for.

      The only thing is, with the value "89493"

      Hours return: 25
      Minutes return: -8

      Is the minutes calculating the left over values?

      I would like to format it like this:

           25 Hours and 8 Minutes

      Which kinda translates to:

           25:08

      so I'm just wondering what is going on with minutes, why it is negative, and if that is the left over minutes from hours?

      Thanks.

      -- shawn
       
      #3
        ehvbs

        • Total Posts : 3320
        • Scores: 112
        • Reward points : 0
        • Joined: 6/22/2005
        • Location: Germany
        • Status: offline
        Re:Convert SECONDS to HOURS and MINUTES in ASP vbscript Thursday, August 12, 2010 5:53 AM (permalink)
        0
        As there is no datatype conversion involved, you should use
        Int() (or Fix()) instead of CInt().

         
        #4
          Shawnm

          • Total Posts : 4
          • Scores: 0
          • Reward points : 0
          • Joined: 3/30/2009
          • Status: offline
          Re:Convert SECONDS to HOURS and MINUTES in ASP vbscript Thursday, August 12, 2010 7:20 AM (permalink)
          0
          thanks for that tip. ill try it out see if its a bit more efficient.

          can you explain the logic behind the (3600 * nHours) calculation?

          also can someone help me find the remainder from the HOURS, as in the above code sample?

          for exampple:

          nTotalSeconds = 89493
          nHours = CInt(nTotalSeconds/3600)
          nTotalSeconds = nTotalSeconds - (3600 * nHours) 

          "89493"
          Hours return: 25
          Minutes return: -8

          why -8? is that the remainder? as i said above I need [0]Hours and [1]Minutes

          Thanks so much.
           
          #5
            ehvbs

            • Total Posts : 3320
            • Scores: 112
            • Reward points : 0
            • Joined: 6/22/2005
            • Location: Germany
            • Status: offline
            Re:Convert SECONDS to HOURS and MINUTES in ASP vbscript Saturday, August 21, 2010 10:58 PM (permalink)
            0
            Use the VBScript Docs to learn about conversion functions
            like CInt(), mathematical/number manipulating functions
            like Int(), Fix(), Round(), and their differences.

            Demo code:

               Dim nTest
               For Each nTest In Array( _
                   60, 3600, 89493, 90, 5400 _
               )
                   Dim nTotalSeconds
                   Dim nHours, nMinutes, nSeconds
                   WScript.Echo nTest
             
                   nTotalSeconds = nTest
                   nHours        = CInt( nTotalSeconds / 3600 )
                   nTotalSeconds = nTotalSeconds - (3600 * nHours)
                   nMinutes      = CInt( nTotalSeconds / 60 )
                   nSeconds      = nTotalSeconds - (60 * nMinutes)
                   WScript.Echo Join( Array( "wrong CInt:", nHours, nMinutes, nSeconds ) )
             
                   nTotalSeconds = nTest
                   nHours        = Int( nTotalSeconds / 3600 )
                   nTotalSeconds = nTotalSeconds - (3600 * nHours)
                   nMinutes      = Int( nTotalSeconds / 60 )
                   nSeconds      = nTotalSeconds - (60 * nMinutes)
                   WScript.Echo Join( Array( "Int:       ", nHours, nMinutes, nSeconds ) )
             
                   nTotalSeconds = nTest
                   nHours        = nTotalSeconds \   3600
                   nTotalSeconds = nTotalSeconds Mod 3600
                   nMinutes      = nTotalSeconds \     60
                   nSeconds      = nTotalSeconds Mod   60
                   WScript.Echo Join( Array( "\ Mod:     ", nHours, nMinutes, nSeconds ) )
             
                   WScript.Echo
             
               Next
             


            output:

             ---------------------------------
             60
             wrong CInt: 0 1 0
             Int:        0 1 0
             \ Mod:      0 1 0
             
             3600
             wrong CInt: 1 0 0
             Int:        1 0 0
             \ Mod:      1 0 0
             
             89493
             wrong CInt: 25 -8 -27
             Int:        24 51 33
             \ Mod:      24 51 33
             
             90
             wrong CInt: 0 2 -30
             Int:        0 1 30
             \ Mod:      0 1 30
             
             5400
             wrong CInt: 2 -30 0
             Int:        1 30 0
             \ Mod:      1 30 0
             
             =================================
             
             
            #6

              Online Bookmarks Sharing: Share/Bookmark

              Jump to:

              Current active users

              There are 0 members and 3 guests.

              Icon Legend and Permission

              • New Messages
              • No New Messages
              • Hot Topic w/ New Messages
              • Hot Topic w/o New Messages
              • Locked w/ New Messages
              • Locked w/o New Messages
              • Read Message
              • Post New Thread
              • Reply to message
              • Post New Poll
              • Submit Vote
              • Post reward post
              • Delete my own posts
              • Delete my own threads
              • Rate post

              2000-2012 ASPPlayground.NET Forum Version 3.9