Dates & Times

In VBA dates are represented as sequential whole numbers.
All the dates from 1 January 1980 to 31 December 2099 are supported.
This whole number represents the number of days since 1 January 1908.
In VBA times are represented as decimal fractions.
All the times from 0:00:00 to 23:59:59 are supported.
This decimal represents the amount of time that has passed since midnight.

Long Data Type

If you are only working with dates (no times) declare these variables as Long (not Date).
Because dates are represented as whole numbers you can use the Long data type for variables that contain dates.
You cannot use an Integer data type because these whole numbers can be greater than 32,767.
The Long data type uses 4 bytes.

Dim l_Long1 As Long 
Dim l_Long2 As Long
l_Long1 = CDate("31 December 2022")
l_Long2 = DateSerial(2022, 12, 31)

Single Data Type

If you are only working with times (no dates) declare these variables as Single (not Date).
The Single data type uses 4 bytes.

Dim sng_Single1 As Single 
Dim sng_Single2 As Single
sng_Single1 = CDate("07:30:00 PM")
sng_Single2 = TimeSerial(19, 30, 0)

Date Data Type

If you are working with both dates AND times declare these variables as Date.
The Date data type uses decimal numbers to represent the date combined with a time.
Negative whole numbers represent dates before 30 December 1899.
The Date data type uses 8 bytes.

Dim dt_Date1 As Date 
Dim dt_Date2 As Date
dt_Date1 = CDate("31 December 2022 07:30:00 PM")
dt_Date2 = #12/31/2022 7:30:00 PM#

© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopNext