Num2Date

From CometWiki

Jump to: navigation, search

NUM2DATE function

Syntax: NUM2DATE(date-serial-number, date-mode)

Discussion: The NUM2DATE function 0 through 3 converts a date serial number to the corresponding date string. NUM2DATE functions 4 through 6 use the current date (ignoring the date-serial-number numeric argument).

The date-serial-number is a 6-digit value (variable or constant) that represents the number of days elapsed from January 1, 1800.

The greatest date covered by this function is December 31, 2299. The following chart demonstrates these values.

 Date serial number   Date string    Notes
 ==================================================
        0             "01011800"     Base date
   182620             "12312299"     Maximum value

When using date-mode 0 through 3, if the value of the date-serial-number is less than 0 or greater than 182620, the NUM2DATE function will return an 8-byte string containing the following values: "000000"

The date-mode is a numeric value that determines the format of the resulting date-string.

 Date-mode   Format of resulting date-string   Length of result
 ==============================================================
 0           MMDDYYYY                               8 bytes
 1           DDMMYYYY                               8 bytes
 2           YYYYMMDD                               8 bytes
 3           YYYYDDMM                               8 bytes
 4 *         Day, DD Mon YYYY HH:MM:SS GMT         29 bytes
 5 **        Day, DD Mon YYYY HH:MM:SS -nnnn -mmmm 37 bytes
 6 ***       HH:MM:SS.mmm                          12 bytes
  • Date-mode 4 is the standard format for Internet dates. This function returns the day of the week (3-character abbreviation), date (formatted as shown, with 3-character abbreviation for the month), and time. The time is expressed as Greenwich Mean Time (GMT) and is based on the current system time.
  • Using the Date Serial Number supplied, Date-mode 5 returns the corresponding Date plus the current system time including the hour adjustments for time zone and daylight savings time. -nnnn is the number of hours + or - different from GMT and -mmmm is the daylight savings time adjustment.
  • Date-mode 6 simply returns the current system time in a 12 byte string formatted as: HH:MM:SS.mmm, where HH is the current hour in 24 hour format, MM is the current minute, SS is the current second, and mmm is the current millisecond. This function may be used in place of the system variable TIME$ where more precision is required.

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

The DATE2NUM function converts a date string into a date serial number.

Also see DATETONUM and NUMTODATE.

History: This function was added in Comet version 504.233 and Comet98 Build 233. Date mode 4 was added in Comet98 Build 266.

Example: The following program demonstrates date-modes 0 through 3:

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

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

SERIAL    DATEMODE      DATESTRING$
==============================================================
72502    0             "07041998"
72502    1             "04071998"
72502    2             "19980704"
72502    3             "19980407"
73598    0             "07042001"
73598    1             "04072001"
73598    2             "20010704"
73598    3             "20010407"
99999    0             "10152073"
182620    0             "12312299"
182621    0             "00000000"

NOTE

Try getting the time as follows:

ATIME$ = Num2Date(0,6)

It is easier, faster, and more accurate.

Personal tools