Date2Num

From CometWiki

Jump to: navigation, search

DATE2NUM function

Syntax: DATE2NUM(date-string,date-mode)

Discussion: The DATE2NUM function converts a date-string to a 6-digit date serial number.

DATE2NUM performs the following steps before converting the date-string:

Adds leading zeroes to the month and day components of the date-string, if necessary

Searches for and removes all non-numeric characters from the date-string, if necessary (for example, removes the "/" symbols between month, day, and year)

Applies the sliding year logic to the date-string, if necessary (see below)

For example, a date-string of "5/1/98" would be converted to "05011998" and then converted to the corresponding date serial number.

The date-mode is a 1-digit numeric value that indicates the format of the date-string (after the above preparatory conversion steps have been performed). The following chart shows the accepted date-mode values:

 Date-mode   Date-string   Date-string format
 Value       format        (with sliding year logic)
 ===================================================
 0           MMDDYYYY      MMDDYY
 1           DDMMYYYY      DDMMYY
 2           YYYYMMDD      YYMMDD
 3           YYYYDDMM      YYDDMM

Note: The date-mode is not an error argument; this function does not change the value of the date-mode. DATE2NUM returns a date serial number that represents the number of days elapsed from a given base date. The base date is January 1, 1800. The greatest date covered by this function is December 31, 2299. The following chart demonstrates these values.

 Date                Date string     Date serial number
 ------------------------------------------------------
 January 1, 1800     "01011800"      0
 December 31, 2299   "12312299"      182620

The DATETONUM function respects the rules for leap years (i.e., the years 1800 and 1900 were not leap years, but the year 2000 was a leap year).

If the date string contains an invalid date, the DATE2NUM function returns a 1- as the date serial number.

The NUM2DATE function converts a date serial numer to a date string.

Also see DATETONUM and NUMTODATE.

Sliding year logic: If the date-string contains a 2-byte year (e.g., "98"), rather than a full 4-byte year (e.g., "1998"), Comet applies the following sliding year logic:

If YY is greater than or equal to the sliding year value (from the system configuration file), the number returned by DATE2NUM uses the sliding century value (it adds this to the YY value). Thus, using the default value, MMDDYY assumes the century to be 1900.

If YY is less than the sliding year value, the number returned by DATE2NUM is the sliding century plus 100 years. If not specified in the configuration file, the default values (1900 and 40) are assumed.

Application note: The DATE2NUM function can simplify the date computation algorithms in your Internet Basic programs. For example, to compute the number of days between two dates, you simply convert both dates to their equivalent date serial numbers, then subtract one value from the other.

History: This function was added in Comet version 504.233 and Comet98 Build 233.

 Example:  LENGTH 12  & LOCAL DATESTRING$
 LENGTH 1.0 & LOCAL DATEMODE
 LENGTH 6.0 & LOCAL SERIAL
 .
 .
 .
 PRINT (0) "Enter a date string:"
 INPUT (0) DATESTRING$
 !
 PRINT (0) "Enter a date mode (0-3):"
 INPUT (0) DATEMODE
 !
 SERIAL = DATE2NUM(DATESTRING$, DATEMODE)
 !
  PRINT (0) "The date serial number is:" ; SERIAL


The following table shows how various input values would be converted by the above program: Input: Result:

DATESTRING$   DATEMODE      SERIAL
==================================
"7/4/98"      0              72502
"7/4/1998"    0              72502
"070498"      0              72502
"07041998"    0              72502
"04071998"    1              72502
"19980704"    2              72502
"19980407"    3              72502
"7/4/01"      0              73598
"7/4/2001"    0              73598
"070401"      0              73598
"07042001"    0              73598
"123456"      0                  1-   (invalid date)
Personal tools