Announcement

Collapse
No announcement yet.

Code to calculate HH:MM:SS between two dates

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Code to calculate HH:MM:SS between two dates

    I want to calculate two date fields and return the difference.

    Something similar to VB's CDATE function but cdate has many
    limitations.

    here is an example on what I'm looking for:

    Code:
    type HoursStruct
         Hours as long
         Minuts as Long
         Seconds as Long
    type
    
    ...
    dim Hours as HoursStruct
    TotalTime "1/15/2001 9:00:00 PM", "1/14/2001 8:30:00 PM", Hours
    ...
    ...
    Anyone have some ideas?

    Thanks

    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

  • #2
    there are a couple of routines in the source code forum that adda a given number of days to a systemtime date... you may be able to adapt these to your needs.

    http://www.powerbasic.com/support/pb...ad.php?t=23049


    i have to admit that i did not search very thoroughly, so it is conceivable that there is code that does exactly what you need... try searching the bbs for "date" or "time".


    ------------------
    lance
    powerbasic support
    mailto:[email protected][email protected]</a>
    Lance
    mailto:[email protected]

    Comment


    • #3
      This was knocked out quickly from misc bits in my toolbox, but it may be of use:
      Code:
      'Functions to do date/time arithmetic and display elapsed times in hh:mm:ss form
      #COMPILE EXE
       
      %SecsInday =  86400
       
      FUNCTION Seconds( strTime AS STRING) AS QUAD
        'convert "hh:mm:ss" to seconds
        FUNCTION = VAL(PARSE$(strTime,":",1))*3600 _
          + VAL(PARSE$(strTime,":",2))*60 _
          + VAL(PARSE$(strTime,":",3))
      END FUNCTION
       
      FUNCTION Days(strDate AS STRING) AS LONG
        'returns number of days since "0-0-0000"(athough that date never actually existed)
        ' for a string in the form mm-dd-yyyy (ie standard PB date$ format)
        LOCAL year AS LONG,month AS LONG,day AS LONG
        LOCAL lyear AS LONG
        LOCAL monthcount AS STRING
        Monthcount = "000031059090120151181212243273304334"
        Lyear = 0
      
        Year = VAL(RIGHT$(strDate,4))
        Month = VAL(LEFT$(strDate,2))
        Day = VAL(MID$(strDate,4,2))
      
        IF Month < 2 THEN
           IF (Year MOD 400 = 0) OR ((Year MOD 4 = 0) AND (Year MOD 100 <> 0)) THEN
              Lyear = 1
           END IF
        END IF
        FUNCTION = (Year*365)+INT(Year/4)-INT(Year/100)+INT(Year/400)+VAL(MID$(Monthcount,Month*3-2,3))+Day-Lyear
      END FUNCTION
       
      FUNCTION DTGseconds(strDTG AS STRING) AS QUAD
       'returns number of seconds since "00-00-0000 00:00:00"
       FUNCTION = Days(PARSE$(strDTG," ",1) )* %SecsInDay + (Seconds(PARSE$(strDTG," ",2)))
      END FUNCTION
       
      FUNCTION strhours(qSeconds AS QUAD) AS STRING
       'convert seconds to "hh:mm:ss"
       FUNCTION = TRIM$(STR$(INT(qSeconds \ 3600))) & ":" _
          & RIGHT$("0" & TRIM$(STR$((qSeconds \60) MOD 60)),2) & ":" _
          & RIGHT$("0" & TRIM$(STR$((qSeconds MOD 60))),2)
      END FUNCTION
                     
      FUNCTION PBMAIN
       LOCAL strTemp AS STRING ,strTemp2 AS STRING
      
       strtemp =  "06-16-2001 11:30:00"
       strTemp2 = "06-17-2001 12:20:00"
       MSGBOX strhours(ABS(DTGseconds(strTemp) - DTGseconds(strTemp2)))
      END FUNCTION

      ------------------
      Check out my free software at http://www.lexacorp.com.pg(all written in PB/DLL)

      Comment


      • #4
        Thanks Guys!

        ------------------
        -Greg
        -Greg
        [email protected]
        MCP,MCSA,MCSE,MCSD

        Comment

        Working...
        X