QUICKBASIC/QBASIC TUTORIAL RE: DATE AND TIME
============================================
By Edward F. Moneo

TABLE OF CONTENTS:
-----------------
A. DATE AND TIME 
B. WEEKS IN A YEAR 
C. LEAP YEAR
D. CONVERTING A DATE TO NUMBER OF DAYS FROM YEAR ZERO


A. DATE AND TIME:
----------------
QB has several functions and statements for referencing date and time.
Only the following two functions will be discussed here:

DATE$
A function which returns a 10 character string of mm-dd-yyyy,
where mm is the month (01-12), dd is the day (01-31),
and yyyy is the year (1980-2099).

TIME$
A function which returns an eight character string of hh:mm:ss,
where hh is the hour (00-23), mm is minutes (00-59),
and ss is seconds (00-59).

BEWARE OF MIDNIGHT (DATE$, TIME$):
It is possible that your program is operating at midnight.

1) If you are 1 second before midnight when obtaining the date,
you should decide whether you want your program to use yesterday's old
date or today's new date. The following code assumes you want to use
the new date:
'get today's date
z$=DATE$
if left$(TIME$,2)="00" then z$=DATE$  'make sure date didn't just roll over
tyyyy$=right$(z$,4)
tmm$  =left$(z$,2)
tdd$  =mid$(z$,4,2)

We first get the date. Then we check if the hour is 00 indicating that
it's just past midnight. If so, we get the date again to make certain
that we have the new date.

By the way, when using military time (00:00 to 23:59),
there is no such thing as 12:00 midnight, there is only 12:00 noon.
Midnight is actually 00:00.

2) If your program needs to obtain both the date and the time,
be careful because the DATE$ and TIME$ are two separate functions,
and it is possible that the DATE$ could end up as yesterday just before
midnight, and the TIME$ is of today, the new day. The following code will
make sure that the date and time correspond to the same day:
'get today's date and time
dt$=DATE$
tm$=TIME$
if left$(tm$,2)="00" then dt$=DATE$

B. WEEKS IN A YEAR:
------------------
Most people say that a year has 52 weeks. Mathematically (365/7) it comes to
a little over 52 weeks. But in reality, looking at a calendar, most years
cover 53 weeks. The first and 53rd weeks usually have only a few days.

However, if the year is a leap year and begins on a Saturday, then that year
spans 54 weeks! The starting Saturday constitutes the first week, and
December 31st, which falls on a Sunday, constitutes the 54th week. This
occurs every 28 years, the last being 1972 and 2000, and the next 2028.

So, what to do with this information? If for any reason you have to set up
arrays, tables or files based on weeks or week number, just set it up to
handle 54 weeks, and you can then ignore the problem.

C. LEAP YEAR:
------------
To determine if a year is leap year, is actually a simple matter.
The rule is as follows:
If the year is evenly divisible by 4, and not divisible by 100,
or if the year is evenly divisible by 400,
then it's a leap year.

Note: Be aware that there were no leap years identified before
      1582 when the Julian calendar was converted to the
      Gregorian calendar.

Sample usage of ISLEAPYEAR function below:
Assume we looked up the maximum number of days per month in a table.
Now, if the month is February and the year is a leap year,
we want to add 1 to the maximum days per month. 

IF MM=2 AND ISLEAPYEAR(YYYY) THEN MAXDAYS=MAXDAYS+1

The following is the function:
Note: It returns -1 when true (leap year), or 0 if false.

FUNCTION IsLeapYear (Z) STATIC
   IsLeapYear = (Z MOD 4 = 0 AND Z MOD 100 <> 0) OR (Z MOD 400 = 0)
END FUNCTION


D. CONVERTING A DATE TO NUMBER OF DAYS FROM YEAR ZERO:
-----------------------------------------------------
Why do we need this? If you want to compute the number of days
between two dates, then you need this. You also need this if
you want to know the day of the week for a particular date.

Note: Be aware that the algorithm below does not take into
      consideration the conversion from the Julian calendar
      to the Gregorian calendar which occurred in 1582.

The following are some subroutines to do these computations.
You may convert them to functions or SUBS as you wish.

DIM DFACTOR      AS SINGLE   'Number of days given date is from day zero.
DIM WEEK.DAY     AS INTEGER  'Day of week value: 1=Sunday....7=Saturday.
DIM ZYY          AS INTEGER  'Value of the 4 digit year.
DIM ZMM          AS INTEGER  'Value of the 2 digit month.
DIM ZDD          AS INTEGER  'Value of the 2 digit day.

COMPUTE.FACTOR:
  DFACTOR=365!*ZYY+ZDD+31*(ZMM-1)  'NOTE: WON'T WORK WITHOUT ! AFTER 365.
  IF ZMM<3 THEN
     DFACTOR=DFACTOR+INT((ZYY-1)/4)-INT(3/4*(INT((ZYY-1)/100)+1)) 
  ELSE
     DFACTOR=DFACTOR-INT(.4*ZMM+2.3)+INT(ZYY/4)-INT(3/4*(INT(ZYY/100)+1))
  END IF
RETURN

COMPUTE.WEEKDAY:
  '* Compute the day of the week:
  WEEK.DAY=DFACTOR-INT(DFACTOR/7)*7    'Modulo 7.
  IF WEEK.DAY=0 THEN WEEK.DAY=7        'WEEK.DAY=1=Sunday.
RETURN

*****


