DATE$() and TIME$(), with adjustment for ZERO A.M.

by Welopez

New users frequently have trouble when working with DATE$() and/or TIME$(). Often this is because they don't realize the values obtained from the PC clock are string values for the following functions:

DATE$()
DATE$("mm/dd/yyyy")
DATE$("mm/dd/yy")
DATE$("yyyy/mm/dd")
DATE$(36980)
TIME$()

ALL REMAINING DATE$ and TIME$ functions return a numerical value. (See the help file topic DATE$() and TIME$().

Should you wish to work with a numerical value from any of the above functions, you will need to extract the LEFT$, MID$, or RIGHT$ characters and then convert using the VAL() function. There are several easy methods to do this, the code below is one example.

         
           'Print current time and date
 
           month=VAL(LEFT$(DATE$("mm/dd/yyyy"), 2)) 'Get the number of current month
           curMonth$="January February March April May June July "+_ 'Name of current month
               "August September October November December"
           day$=MID$(DATE$("mm/dd/yyyy"),4,2) 'Get dd as string
           year$=RIGHT$(DATE$("mm/dd/yyyy"),4) 'Get yyyy as string
           today$=WORD$(curMonth$, month)+", "+ day$+ ", "+ year$ 'Print desired format

           now$=TIME$()
           hh= VAL(LEFT$(now$,2))
           IF hh >= 12 THEN 'Is it a.m. or p.m.?
               IF hh >=13 THEN
                   hh=hh-12 'Correct the two leading characters
                   now$=STR$(hh)+RIGHT$(now$, 6) 'Concatenate the correct now$
               END IF
               now$=now$+" p.m."
           ELSE
               IF hh<1 THEN 'If it is zero hours in the morning
                   hh=12
                END IF
               now$=STR$(hh)+RIGHT$(now$, 6)+" a.m."
           END IF

           PRINT now$
           PRINT today$
           END
         
       

In this example, the user wants to print the full name of the current month, but DATE$("mm/dd/yyyy") returns only the three letter abbreviation for the month. I obtained the numerical value for the current month by taking only the left two characters of DATE$(), converting the characters to a number (which will ignore any leading zeros), and then searching for WORD$(month) in a string of all months. The WORD$(month) returns the month word in the string.

In a similar manner, day$ and year$ were extracted from DATE$("mm/dd/yyyy"). Once the string values had been determined, today$ could be formatted as desired. In Europe it is common to print the day before the month; in the US we print the month before the day.

Determining A.M. or P.M. from TIME$() was just as easy. If value of the two characters of LEFT$(now$,2) is equal to or greater than 12, the current time is P.M., ELSE it is A.M. Later in the afternoon, when the time may be 16:30 (24 hour format), we don't want to print 16:30 P.M., so we subtract 12 hours from hh and print the correct time 4:30 PM, the STR$ value of hh plus the right six characters of now$, which includes the colons for hours, minutes, and seconds.

Should the current time be between midnight and 1 A.M., we need to correct for zero hours, so we use IF hh<1 THEN add 12 hours to hh, and finally the corect A.M./P.M determination is concatenated to now$.

This should take care of any problems you may encounter when using DATE$() and TIME$(). The important thing to remember is many of the functions you will use are string functions and must be evaluated as such.

OMG! What a dumb bunny mistake! I forgot to include the final two lines to print the output. I guess it must be this sumtimer's thing. Take it from me, when it comes to getting older - - DON'T!