How to Convert Text Number String into Date Format

Last reviewed: June 27, 1995
Article ID: Q108327
The information in this article applies to:
  • Microsoft FoxPro for Windows, versions 2.5, 2.5a, and 2.5b
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, 2.5a, and 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 reference words: FoxDos FoxWin 2.00 2.50 2.50a 2.50b convert
KBCategory: kbprg kbcode
KBSubcategory: FxprgBrowse


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: June 27, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.