Visual Basic Notes
Visual Basic Notes
One of the places where Visual Basic really shines is in the handling of dates using
the Date data type. You can display a date in any one of several standard date formats,
but internally it is represented as a serial number specifying the number of days since
December 31, 1899, with negative values used for dates before then. The decimal
portion of the value can be used to represent a time of day, but that is beyond the
scope of this tip.
The way dates are represented makes it really easy to compare dates, but some Visual
Basic programmers don't realize how easy it is and will go to unnecessary complexity
to perform a comparison. There's no need to extract the year, month, and day
individually for comparison--rather you can simply compare dates directly using the
standard comparison operators.
For example, the date June 30, 2004 is represented by the serial number 38168.
Comparing it with another date to see if it is earlier, the same, or later is really just
comparing the serial numbers to see if one is smaller, the same, or larger than the
other. Here's an example that executes one block of statements if the date stored in the
type Date variable MyDate is earlier than January 1, 2000 and another block if it is the
same or later than that date.
If MyDate < #1/1/2000# Then
' Statements here are executed if MyDate is earlier.
Else
' Statements here are executed if MyDate is the same or later.
End If
This is just one example of the flexibility of Visual Basic's Date data type.