Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

DMY(), MDY()

DMY() returns a character string in the form day-month-year, and MDY returns month-day-year, when supplied with a date or datetime variable.

Usage

lcDateToDisplay = DMY( dDate | tDateTime )
lcDateToDisplay = MDY( dDate | tDateTime )

Neither of these two functions has much going in the user-friendly department. Both DMY() and MDY() precede a single-digit day with a zero, and both display two-digit years if CENTURY is set OFF. We would typically use these functions to display a date on a report, and both fall short of our standards for that task.

The following function produces a much prettier version of DMY(), always returning a four-digit year field and not preceding single-digit values with a zero:

* GoodDmy.PRG - return a prettier DMY value
* Parameter: tdDate - a date or datetime value
*            default - today's date
* Returns: String of form DD Mmmmmm YYYY
PARAMETER tdDate
IF EMPTY(tdDate) OR NOT INLIST(type('tdDate'),"D","T")
  tdDate = DATE()
ENDIF
RETURN PADL(DAY(tdDate),2) + " " + ;
       CMONTH(tdDate) + " " + ;
       STR(YEAR(tdDate),4)

MDY() returns a character string in the form MMM DD, YY (or YYYY, depending on SET CENTURY). Like the DMY() function, this one isn’t too smart, preceding single-digit dates with a zero, and displaying years as two-digit numbers if CENTURY is set OFF. A better version is:

* GoodMDY.PRG - return a prettier MDY value
* Parameter: tDate - a date or datetime value
*            default - today's date
* Returns: String of form Mmmmmm DD, YYYY
PARAMETER tdDate
IF EMPTY(tdDate) OR NOT INLIST(TYPE('tdDate'),"D","T")
  tdDate = DATE()
ENDIF
RETURN CMONTH(tdDate) + " " + ;
       LTRIM(STR(DAY(tdDate))) + ", " + ;
       STR(YEAR(tdDate),4)

Example

? DMY({03/18/1954})  && returns "18 March 1954"
? DMY({01/01/01})    && returns "01 January 01" with CENTURY OFF
? MDY({03/18/1954})  && returns "March 18, 1954"

See Also

Day(), Dow(), Date(), DateTime(), GoMonth(), Month(), Set Century, Set Date, Set Mark To, Year()