Date Formatting With dBASE III ------------------------------ by Chris White There are often occasions when you may wish to display date variables in formats other than those allowed by the SET DATE command in version 1.1. (See the dBASE III version 1.1 manual insert, "CORRECTIONS TO THE MANUAL AND PRODUCT CHANGE SUMMARY" page 5 and 6, for the various date formats allowed.) Specifically, you may wish to display the date in a format such as January 25, 1984. This is useful for check printing, invoice writing, report formats conforming to standards, or merely for aesthetic reasons. The most common method of converting a date variable to a formatted string consists of using the SUBSTR, STR, and various date functions in combination. As an example, let us convert the system date by assigning it to a character string variable and then displaying the variable with the @...SAY command. Assuming the system date is 01/25/84, the desired output will look like "January 25, 1984." The statement that makes the conversion is as follows: datestrg = CMONTH(DATE())+" "+STR(DAY(DATE()),2)+", "+; STR(YEAR(DATE()),4) To understand this expression, let us examine it in some detail. The memory variable datestrg will contain the formatted date string. The first portion of the expression, CMONTH(DATE()) returns the month name as a character string, "January," in our example. A blank character is concatenated onto the end of the month name with +" ". The second portion of the expression, STR(DAY(DATE()),2) returns the numeric day of the month as a character string. A comma and a blank are concatenated with +", ". We have so far "January 25,". The last portion of the expression, STR(YEAR(DATE()),4) returns the year as a four-character string. The results of this last expression are concatenated to the month and day. The memory variable, datestrg, will at this point contain "January 25, 1984". Finally, to display the string to the screen or printer execute the command: @ 10,10 SAY datestrg It is not necessary to assign the result of the expression to a memory variable in order to display it. The @...SAY could well look like: @ 10,10 SAY CMONTH(DATE())+" "+STR(DAY(DATE()),2)+", "+; STR(YEAR(DATE()),4) Additionally, this date expression can be used as the field contents in either the REPORT or LABEL FORM or any other display command that accepts a character string expression. For other formatting algorithms please refer to the example that follows. >>> General Date Formats For those who have been building a general purpose library of useful routines, the following procedure may be a beneficial addition. This procedure can be used as a stand-alone command file or as a procedure in a PROCEDURE file. It uses the PARAMETERS clause to allow the routine to be general and useful for a variety of different programs without change. For further discussion on the use of the PARAMETERS clause, please refer to the dBASE III manual page 4-76 for version 1.0 or page 4-83 for version 1.1. The construction of the routine is quite simple. It takes two inputs, a date variable to format and a numeric code designating the format desired, and produces one output, a character variable containing the formatted date string. The syntax to call the routine is as follows: STORE " " TO finv_date ---------------------- |__________________________ Receiving variable must exist prior to call DO fdate WITH inv_date,5,finv_date ----- ---- ------- - --------- | | | | |_____ Character variable to | | | | receive formatted date | | | | | | | |___________ Format code | | | | | ----------------- Date variable to format | | | ------------------------ Specifies PARAMETER list | ----------------------------- Format routine name The formats that are supported are as follows: 1. Mon DD, YYYY 2. Mon YYYY 3. Mon DD 4. Month DD, YYYY 5. DD-Mon-YYYY >>> Program for General Date Formats * Program..: Fdate.PRG * Author...: Chris White * Date.....: January 25, 1985 * Note(s)..: This program takes a date variable passed to it * with the PARAMETERS phrase and returns the date * as a formatted character string. * PARAMETERS date,code,datestrg DO CASE CASE code = 1 * ---"Jan 25, 1984" datestrg = SUBSTR(CMONTH(date),1,3) + ' ' +; STR(DAY(date),2) + ', ' + STR(YEAR(date),4) CASE code = 2 * ---"Jan 1984" datestrg = SUBSTR(CMONTH(date),1,3) + ' ' +; STR(YEAR(date),4) CASE code = 3 * ---"Jan 25" datestrg = SUBSTR(CMONTH(date),1,3) + ' ' +; STR(DAY(date),2) CASE code = 4 * ---"January 25, 1984" datestrg = CMONTH(date) + ' ' +; STR(DAY(date),2) + ', ' + STR(YEAR(date),4) CASE code = 5 * ---"25-Jan-84" datestrg = STR(DAY(date),2) + '-' +; SUBSTR(CMONTH(date),1,3) +; '-' + SUBSTR(STR(YEAR(date),4),3,2) OTHERWISE datestrg = 'Error' ENDCASE RETURN * EOF: Fdate.PRG --End--