Date and time processing

REXX provides two functions for date and time processing, date() and time(), which generate the following default values if used without any arguments;

   say date() -> dd mmm yyyy e.g. 15 Aug 1990
   say time() -> hh:mm:ss e.g. 11:23:45

The default actions of date and time can be overridden by specifying and option as an argument. For a complete description of these see either the CMS HELP files or the System Product Interpreter guide.

Getting the day, month and year in separate REXX variables

It is often necessary to get the current day, month and year, or time in hours, minutes and seconds for a variety of purposes. The following examples show a common method of coding in order to achieve this. These methods all cause a number of function calls to be made when in fact a single 'parse' will suffice.

Conventional method

   TODAY=date('E') /* European format */
   DD=substr(TODAY,1,2)
   MM=substr(TODAY,4,2)
   YY=substr(TODAY,7,2)

or even worse!

   DD=substr(date('E'),1,2)
   MM=substr(date('E'),4,2)
   YY=substr(date('E'),7,2)

Suggested method

   parse value date('E') with DD '/' MM '/' YY

If the month name is required in full (MONTHNAME), but only its sequential number in the year is available (MON), then the following code is often encountered;V

Conventional method

   MONTH.1='January'
   MONTH.2='February'
   etc
   MONTH.12='December'
   do I=1 to 12
     if MON=MONTH.I then MONTHNAME=MONTH.I
   end

Suggested method

   MONTHNAME=word('January February','March April May June July','August September October','November December',MON)