Getting a Date with DATE$()

by Welopez

Create a folder named "Working With DATE$". We're going to be working with 4 snippets of code, and 3 text files for data. All snippets should be run in order, and from the same folder.

There will be many times while coding when you need to display a date, either today, or a date in the past or future. JB provides 7 formats to display dates. Although the DATE$() function appears to yield a string, it is in fact a function and two of the return values are numerical, while the remaining five returned values are strings. See the helpfile topic DATE$().

The current date is displayed when you use DATE$(), with nothing inside the parenthesis. If you use DATE$("07/20/1969"), the return value is a number, also called the serial number for the date. You get a number when you type DATE$("07/20/69"), and the year is assumed to be the CURRENT CENTURY. Type these lines into your IDE and run:


PRINT DATE$("07/20/69") 'The date of the first moon landing.
PRINT DATE$(61562)      'The serial number date of the first moon landing.
END

Keep this in mind so you will not fall into the Y2K error. Remember, when we use a string value with DATE$(), we return a number. When we use a numeric value with DATE$(), we return a string.

When I was a very young man, I discovered there was a difference between male and female, and remarked "Vive la difference!" Different does not always mean better, because America still does not commonly use the metric system, which is simply a matter of moving a decimal point to convert to a larger or smaller unit.

We don't use the same notation when writing dates, either. Because those gnomes in Redmond, WA, have powerful influence over Worbles and PC operation, the default of DATE$() uses American notation, Jan 1, 2006, for example. In much of the world, dates are written using European notation, 1 Jan, 2006, for example.

To make it possible for us to use either method, PC's use a unique serial number for every date, beginning with 01/01/1901. DATE$("days") returns the number of days since 01/01/1901. It is a number and you can do math with that number. DATE$("07/20/1969") is also a number, and you can do math with that number as well. DATE$("mm/dd/yyyy") returns the serial number for the specified date. It is most useful for purposes of sorting by date.

The following code provides example displays of Jul 20, 1969, the date Apollo 11 landed on the moon. It includes functions to display the date in either American notation, or the notation used by the rest of the world.

SNIPPET #1


'Making a DATE with DATE$()
'by Welo, 03/05/2006

PRINT "Todays date is "; DATE$() 'The serial number of TODAY
PRINT

'===== Get the serial number for the date of the first moon landing
apollo11=DATE$("07/20/1969")
PRINT "The serial number for the landing of Apollo 11 is "; apollo11
PRINT

'===== Print the date using American format
myDate$=american$(apollo11) 'FUNCTION american$
PRINT "Neil Armstrong first set foot on the moon on ";myDate$
PRINT

'===== Print the date using European format
myDate$=european$(apollo11) 'FUNCTION european$
PRINT "Neil Armstrong first set foot on the moon on "; myDate$
PRINT

'===== Print the date including month
mm$=month$(apollo11) 'FUNCTION month$
PRINT "Apollo 11 landed on the Moon on "; mm$
PRINT

END '<<===== End of program execution

'===== American date format is the default, no need for manipulation
FUNCTION american$(apollo11)
    american$=DATE$(apollo11)
END FUNCTION

'===== Manipulate DATE$() to display European date format
FUNCTION european$(apollo11)
    european$=DATE$(apollo11)
    european$=MID$(european$,4,2)+"/"+_ 'Gets the day
    LEFT$(european$,2)+"/"+_             'Gets the month
    RIGHT$(european$,4)                 'Adds the year
END FUNCTION

'===== Manipulate DATE$() to display the name of the month
FUNCTION month$(apollo11)
label$="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
mm$=DATE$(apollo11) 'Date of moon landing as string
mm=VAL(LEFT$(mm$,2)) 'Get the numerical value of this month
month$=WORD$(label$,mm);" ";_ 'Find the WORD$ in label$
    MID$(mm$,4,2);", "; RIGHT$(mm$, 4)
END FUNCTION

Because apollo11 is a numerical value, it can be easily converted to a string for a given date, then the string can be manipulated to display American or European notation, and even show the name of the month!

Only two of the formats of DATE$() return a number. The remaining values are string representations of a date. If you sort your data by date using the string value, your records will be ordered first alphabetically: Apr, Aug, Dec, Feb, Jan, Jul, etc, then day, and finally year. Months will be grouped together with years thown in willy-nilly. Obviously this is not what you wanted to do, so it is better to save your data using DATE$("mm/dd/yyyy"), which is the serial number for your specified date. Input your data from a file or data statements, sort it by serial number, and then output DATE$(date) to display mm/dd/yyyy in chronological order.

Here is a snippet to create 25 random dates as strings, and save to "myTestFile.txt.

SNIPPET #2


'Create test file for date$
OPEN "myTestFile.txt" FOR OUTPUT AS #1
FOR k=1 to 25
    mm$=STR$(INT(RND(0)*12)+1)
    dd$=STR$(INT(RND(0)*30)+1)
    yy$=STR$(INT(RND(0)*6)+2000)
    outString$=mm$+"/"+dd$+"/"+yy$
    PRINT #1, outString$
NEXT k
CLOSE #1

END

I know, we just talked about serial numbers of dates, but we need a test file to work with so we can convert date strings to serial numbers.

Now that we have "myTestFile.txt" filled with 25 random dates, we can open it with this code and convert to the appropriate serial number for the given dates, then save the serial numbers to "dateSN.txt". (Hey, we're getting lots of practice with sequential files.)

SNIPPET #3


'Convert mm/dd/yyyy to sn(date)
OPEN "myTestFile.txt" FOR INPUT AS #1
OPEN "dateSN.txt" FOR OUTPUT AS #2
WHILE EOF(#1)=0
    INPUT #1, a$
    newDate=DATE$(a$)
    PRINT #2, newDate
WEND
CLOSE #2
CLOSE #1

END

Now we've converted all those strings to serial numbers, and saved as "dateSN.txt". You'll do a lot of converting when working with dates, so it's a good idea to practice these until you understand them.

Next we'll open "dateSN.txt," and sort those numbers. What could be easier? This snippet gets all the serial numbers from the file, sorts them, and prints them to "datesOut.txt".

SNIPPET #4


'SORT dateSN.txt
OPEN "dateSN.txt" FOR INPUT AS #1
WHILE EOF(#1)=0
    INPUT #1, dummy
    cnt=cnt+1 'Count the dates in the file
WEND
CLOSE #1

DIM date(cnt) 'DIM the array for dates
OPEN "dateSN.txt" FOR INPUT AS #1
WHILE EOF(#1)=0
    k=k+1
    INPUT #1, date(k) 'Load the array
WEND
CLOSE #1

'===== Sort the array
FOR k=1 TO cnt
    FOR i=1 TO (cnt-1)
        IF date(i)>date(i+1) THEN
        temp(date)=date(i)
        date(i)=date(i+1)
        date(i+1)=temp(date)
        END IF
    NEXT i
NEXT k
'===== Sort complete
'===== Print the sorted array
OPEN "datesOut.txt" FOR OUTPUT AS #3
FOR k=1 to cnt
    PRINT #3, DATE$(date(k))
NEXT k
CLOSE #3

END

You can use all three snippets in one program, providing you keep your variable names and device ID's sorted out. As shown in "textOut.txt", all dates will be sorted chronologically and grouped by years... which is usually the way you want to see them anyway.

Okay, now that we've had fun with DATE$(), I have to see about getting a date with a member of the opposite gender. (Coders do not live by keyboard alone!)