DTOC() Returns Unexpected Results When Used with Dates

Last reviewed: March 20, 1998
Article ID: Q99618
1.02 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 1.02, 2.0, 2.5, and 2.5a
  • Microsoft FoxPro for Windows, versions 2.5 and 2.5a
  • Microsoft FoxBASE+ for MS-DOS, version 2.1
  • Microsoft FoxBASE+ for Macintosh, version 2.01

SUMMARY

When comparing date fields and character strings so that the results of the comparison reflect a given range of dates, the comparison must be done by using the CTOD(<string>) command, rather than using the DTOC(<date field>) command.

MORE INFORMATION

Consider the following code:

   date_fld=03/01/93  && An actual date field.
   comp_str="04/01/90"  && The string to compare.

   BROWSE FOR DTOC(date_fld)>comp_str

The BROWSE command above does not return 03/01/93 as being greater than 04/01/90 because the strings are compared from left to right and the month takes precedence in a straight string comparison regardless of the year. To obtain the correct result, the strings must be compared as date to date rather than string to string. To perform this type of comparison, modify the BROWSE command to read as follows:

   BROWSE FOR date_fld>CTOD(comp_str)

This type of BROWSE command ensures the year is compared before the month, and therefore the desired results are returned.


Additional reference words: FoxDos FoxWin 2.00 2.10 2.50 2.50a
KBCategory: kbprg
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: March 20, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.