Microsoft KB Archive/101796

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

How to Convert Time as "HH:MM:SS" to a Numeric Value

ID: Q101796

2.00 2.50 2.50a | 2.50 2.50a

MS-DOS          | WINDOWS

kbprg The information in this article applies to:

  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, and 2.5a
  • Microsoft FoxPro for Windows, versions 2.5 and 2.5a

To convert a time string of the form "HH:MM:SS" to a numeric value, you must create a user-defined function (UDF). The following code is provided as an example.

   * TTON(<expC>)
   * Convert <expC> to a number of hours
   * Return value - Numeric
   * <expC> is a string of the form "HH:MM:SS". If <expC> doesn't
   * contain two colons (::), or time values are not in the range,
   * TTON returns -1.

   FUNCTION TTON
   PARAMETERS timestr
   PRIVATE pos1,pos2,hours,minutes,seconds

   * Get position of first and second occurrences of ':'
   pos1=at(':',timestr,1)
   pos2=at(':',timestr,2)

   * Error checking
   IF pos1==0 OR pos2==0
      RETURN -1
   ENDIF

   * Convert hh, mm and ss to numeric values and check for valid range
   hours = VAL(timestr)
   IF hours < 0 or hours > 24
      RETURN -1
   ENDIF
   minutes = VAL(SUBSTR(timestr,pos1+1))   && from 1st ':' to 2nd ':'
   IF minutes < 0 or minutes > 59
      RETURN -1
   ENDIF
   seconds=VAL(SUBSTR(timestr,IIF(LEN(timestr)>=pos2+1,pos2+1,0)))
   IF seconds < 0 or seconds > 59
      RETURN -1
   ENDIF

   * Convert minutes and seconds to decimal values
   minutes=minutes/60
   seconds=(seconds/60)/60

   * Return total value of hours, minutes and seconds
   RETURN ( hours+minutes+seconds )

Additional reference words: FoxWin FoxDos 2.00 2.50 2.50a NTOT KBCategory: kbprg KBSubcategory: FxprgGeneral

Keywords          : FxprgGeneral 
Version           : 2.00 2.50 2.50a | 2.50 2.50a
Platform          : MS-DOS WINDOWS

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