Date functions overview
This page discusses three ways to fill a date variable:
- Use Date constant syntax
- Create date from year, month and day - DateSerial
- Convert string to date - DateValue
Additionally, it links to pages that cover all kind of date functions related to calculations:
Setting date values
Use Date constant syntax
You can use fixed dates in your VBA code by putting the date between # #, as below.
Const cdtMyDate As Date = #12/31/2001#
Create date from year, month and day - DateSerial
The DateSerial function fills a date variable from year, month and day - see code below.
Dim iYear As Integer: iYear =
Dim iMonth As Integer: iMonth =
Dim iDay As Integer: iDay =
Dim dt As Date
dt = DateSerial(Year:=iYear, Month:=iMonth, Day:=iDay)
Convert string to date - DateValue
The DateValue function converts a string to a date. The code below interprets the string as 2/12/2009.
Dim strDate As String: strDate = "February 12, 2009"
Dim dt As Date
dt = DateValue(strDate)
Note |
---|
|
Below image shows the Code VBA add-in support for VBA Date procedures.