Knowledge Base

Elapsed Time Example Involving Different Days

Article ID: 115989

Article Last Modified on 12/3/2003


APPLIES TO


This article was previously published under Q115989

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 query words: VFoxWin FoxWin FoxDos calculate udf

Keywords: kbcode KB115989