[Previous] [TOC] [Next]

The Date Data Type

Visual Basic has had a dedicated Date data type since version 4 and, prior to that (in versions 2 and 3) a Date Variant type with the same storage pattern. Dates can be declared and used like this:

Dim dteMyDate     As Date

  dteMyDate = DateSerial(1998, 2, 12)

Or perhaps

dteMyDate = #2/12/98#

The Date data type is actually stored as an IEEE double-precision floating point value, 8 bytes long. The data stored can represent dates from January 1 100 up to December 31 9999. Days are stored as whole numbers, with zero being December 30 1899. Dates prior to this are stored as negative values, those after are positive. In the example above, February 12 1998 is stored as 35838. You can test this outcome with the following code:

MsgBox CDbl(DateSerial(1998, 2, 12))

The Date data type is also able to hold time information. Hours, minutes, and seconds are held as fractions, with noon represented as 0.5. If we take the number of seconds in a day, 86400, and divide that into 1, the answer is the fraction equal to one second: 0.000011574…. The table below shows the minimum, default, and maximum values that can be stored in a variable declared as a Date.

Date Value Stored
Minimum Value January 1 100 00:00:00 -657434
Default Value December 30 1899 00:00:00 0
Maximum Value December 31 9999 23:59:59 2958465.99998843

As we can see, there is nothing wrong with the way Visual Basic stores dates. Its method is both compact and Year 2000 compliant. For example, 8 bytes would store only the date if encoded as an alphanumeric CCYYMMDD. In effect, the Date data type allows us to store the time for free.

[Previous] [TOC] [Next]