Elapsed Time Example Involving Different Days

Last reviewed: April 30, 1996
Article ID: Q115989
The information in this article applies to:
  • Microsoft Visual FoxPro for Windows, version 3.0
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, 2.5a, 2.5b, 2.6
  • Microsoft FoxPro for Windows, versions 2.5, 2.5a, 2.5b, 2.6
  • Microsoft FoxPro for Macintosh, versions 2.5b, 2.5c

SUMMARY

The function shown below determines the number of seconds that have elapsed between two different times. This function will take into account switching to the next day.

MORE INFORMATION

   PROCEDURE elaptime
   PARAMETER m.startdate, m.enddate, m.starttime, m.endtime
   * pass startdate, an 8-character starttime (HH:MM:SS), an enddate,
   * and an 8-character endtime
   * perform parameter type checking: return value of -1 indicates
   * invalid parameter type
   * Example of using the function elaptime:
   * TempVariable=ElapTime({01/01/94},{01/02/94},"00:01:00",00:01:00")

   IF TYPE('m.startdate') # "D" .OR. ;
       TYPE('m.enddate') # "D" .OR. ;
       (TYPE('m.starttime') # "C" .AND. LEN(m.starttime) # 8) .OR. ;
       (TYPE('m.endtime') # "C" .AND. LEN(m.endtime) # 8)
       RETURN -1
   ENDIF

   * if startdate > enddate, or same date and starttime > endtime,
   * return 0
   IF m.startdate > m.enddate .OR. (m.startdate = m.enddate .AND. ;
        m.starttime > m.endtime)
       RETURN 0
   ENDIF

   #DEFINE secsperday 86400

   * determine whether the endtime is earlier or later than the
   * starttime
   IF m.endtime >= m.starttime
       m.elapsecs = (m.enddate - m.startdate) * secsperday + ;
           (time2secs(m.endtime) - time2secs(m.starttime))
   ELSE
       m.elapsdays = 0
       IF m.enddate - m.startdate > 0
           m.elapsdays = m.enddate - m.startdate - 1
       ENDIF
       m.elapsecs = (m.elapsdays * secsperday) + ;
           (secsperday - time2secs(m.starttime)) + ;
           time2secs(m.endtime)
   ENDIF

   RETURN m.elapsecs


   FUNCTION time2secs
   * passed a C8 HH:MM:SS string
   * returns the number of seconds into the day this is
   PARAMETER m.passedtime

   m.retsecs = VAL(RIGHT(m.passedtime, 2)) + ;
       60 * VAL(SUBSTR(m.passedtime, 4, 2)) + ;
       3600 * VAL(LEFT(m.passedtime, 2))
   RETURN m.retsecs


Additional reference words: VFoxWin 3.00 FoxWin FoxDos 2.60 calculate udf
KBCategory: kbprg kbcode
KBSubcategory: FxprgGeneral


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 30, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.