| ACADEMIC COMPUTING and COMMUNICATIONS CENTER | |||||||||
| |||||||||||||||||
Using ISO 8601 (yyyy-mm-dd) Dates in Programs | |||||||||||||||||
|
Now that you're convinced that using ISO 8601 dates -- dates written in the format yyyy-mm-ddd -- is a good thing, here's how to use them in the major programming languages and statistical software packages. (Not convinced yet? See ISO 8601: The Right Format for Dates. For instructions on how to set ISODATES in your personal computer, see Setting ISO 8601 Dates in Personal Computer Operating Systems.) |
|||||||||||||||||
| SPSS | |||||||||||||||||
|
SPSS stores dates internally as the number of seconds since midnight, October
14, 1582, which was the first day of the Gregorian Calendar. The built-in variable
$time contains the current date and time in that format, as an integer quantity.
(Actually, all quantities in SPSS are stored as 64-bit floating point numbers.
This allows for 56 bits of precision, which avoids the 2037 problem that Unix
has with its 32-bit integer.)
SPSS has several date and time format codes that will print these huge numbers as human-readable dates and times, such as DATE11 which is the British-English literary style, but it does not have one for ISO format. You can construct a string variable containing the date in ISO format with the following SPSS language commands:
/* Assume the date is contained in SPSS variable "dt" */
STRING isodate (A10).
COMPUTE isodate = CONCAT(STRING(XDATE.YEAR(dt),N4.0),'-',
STRING(XDATE.MONTH(dt),N2.0),'-'
STRING(XDATE.MDAY(dt),N2.0)).
To read raw data containing dates in ISO date format, first read it into three Scratch Variables, and then use an Aggregation Function to create a regular permanent variable in SPSS date/time internal format. Scratch variables have names beginning with #, and are only temporary work variables that are not saved in the System File. DATA LIST / #yr 1-4 #mo 6-7 #da 9-10. COMPUTE dt = DATE.MDY(#mo,#da,#yr). BEGIN DATA. 1998-06-23 2000-02-29 2001-01-02 END DATA.Back to Contents |
|||||||||||||||||
| SAS | |||||||||||||||||
|
SAS stores dates internally as the number of seconds since midnight, January 1,
1960. Dates before then are stored as negative numbers. The built-in variable
contains the current date and time in that format, as an integer quantity. (Actually,
all quantities in SAS are stored as 64-bit floating point numbers. This allows
for 56 bits of precision, which avoids the 2037 problem that Unix has with its
32-bit integer.)
SAS has several date and time format codes that will print these huge numbers as human-readable dates and times, but it does not have one for ISO format. You can construct a string variable containing the date in ISO format with the following SAS language commands: (under construction) |
|||||||||||||||||
| Rexx Programs | |||||||||||||||||
|
This information applies to all versions of Rexx - VM, OS/2, AIX, or Regina. Some
of the newest versions of Rexx contain extensions which make the following unnecessary,
but those extensions should be avoided for the sake of portability.
The Rexx DATE function returns the date in one of a variety of formats depending on the character constant you pass it. The default format, called Normal, is the British-English literary format, "23 Jun 1998", reflecting the fact that Rexx's author, Mike Cowlishaw, is an Englishman. Some of Rexx's date formats should be avoided, such as Century, which gives day number within the century and will go to 0 in 2000, or Usa, which is the American mm/dd/yy, with a 2-digit year. The Standard format, yyyymmdd, is the best, since it is very close to ISO. All we need to do is add the dashes. This is easily done by:
isodate = translate('year-mn-dt',date('S'),'yearmndt')
If you want to do date arithmetic in Rexx, you should use the Base format, which is the number of days since January 1, 0001. A variety of routines are available for converting between the different Rexx date formats. Some newer versions of Rexx, such as CMS 14, contain extensions to the DATE function which can perform these conversions. More routines are available on The Rexx Language Homepage |
|||||||||||||||||
| C Programs | |||||||||||||||||
|
(Watch this space!) |
|||||||||||||||||
| Fortran Programs | |||||||||||||||||
The Fortran built-in subroutine DATIM returns an eight-member integer
array containing the current date and time broken down into individual elements.
The following Fortran code fragment obtains today's date and writes it in ISO
format:
INTEGER DATP(8)
CALL DATIM(DATP)
WRITE (6,44) DATP(8),DATP(7),DATP(6)
44 FORMAT(1X,I4.4,'-',I2.2,'-',I2.2)
Back to Contents |
|||||||||||||||||
| Perl Programs | |||||||||||||||||
Perl's built-in function time returns the number of seconds since
Midnight, January 1, 1970, GMT. It's conversion functions gmttime, localtime
will convert such a quantity to date and time quantities.
To write this time stamp to a file in ISO format: (under construction)
This page last updated 1998-09-24. Please send comments and reports of broken
links to the author: Roger Deschner |
|||||||||||||||||
| Using ISO 8601 Dates | Previous: 1. What They Are | Next: 3. In Output |
| 2001-12-21 ACCC Documentation |
|