How to Convert Text Number String into Date Format

ID: Q108327


The information in this article applies to:
  • Microsoft FoxPro for Windows, versions 2.5, 2.5a, 2.5b
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, 2.5a, 2.5b

The following code example demonstrates how to use the STUFF() function to correct a date string that is in the form of a number or character so that it can be inserted in a date field or used with a date function.

   *** Code Example

   newdate = 62793                 && This is a date number without a ;
                                      leading zero
   newdate = STR(newdate)          && Converts the number into a string

   IF LEN(ALLTRIM(newdate)) = 5    && Checks to see if the character
   newdate="0"+ALLTRIM(newdate)    && string is 5 characters and adds a
   ENDIF                           && leading zero if needed

   newdate=STUFF(ALLTRIM(newdate),3,0,"/")   && inserts a / for the 3rd
                                             && character
   newdate=STUFF(ALLTRIM(newdate),6,0,"/")   && inserts a / for the 6th
                                             && character
   SET CENTURY ON
   WAIT WINDOW MDY(CTOD(newdate))

   && newdate can now be inserted in a date field of the database.

   CREATE TABLE datetemp.dbf (name C(12), fixdate D, TYPE C(10))
   APPEND BLANK
   REPLACE name WITH "It Work’s!",  fixdate WITH CTOD(newdate), TYPE WITH
   "cool"
   BROWSE

   && This code closes the database and deletes it.

   IF USED('datetemp')
        USE IN datetemp
   ENDIF
   IF FILE ('datetemp.dbf')
        DELETE FILE datetemp.dbf
   ENDIF 

Additional query words: FoxDos FoxWin convert


Keywords          : 
Version           : 
Platform          : 
Issue type        : 


Last Reviewed: August 23, 1999
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.