Date Statement
Sets the current system date.
Syntax
Date = dateexpression
Parts
- dateexpression: Required. Any expression that can represent a date,
including date literals (
#1/1/2025#), date serial values, or string expressions that parse as dates.
Return Value
None. This is a statement, not a function.
Remarks
The Date statement sets the system date to the value of dateexpression.
In VB6 on Windows this modifies the real system clock and requires
administrator privileges. In this runtime the behavior depends on the
allow_system_clock flag:
- Default (system clock allowed): The date is written to the real
system clock via platform APIs. Subsequent Date and Date$ calls
return the real system date.
- Internal-only mode: The date is stored in an offset-based mock
clock that advances in real time from the set point. The real system
clock is never modified.
- Date Storage: Internally, dates are stored as Double values
representing the number of days since December 30, 1899.
- Date Range: January 1, 100 through December 31, 9999.
- Permissions: On real VB6, changing the system date requires
administrator privileges.
- Date Function: Use the Date function (without assignment) to
retrieve the current system date.
- Date$ Function: Use Date$ to retrieve the current date as a string.
- Error Handling: Invalid date values generate a runtime error (error 13,
type mismatch).
Examples
Set Date to Specific Value
Date = #1/1/2025#
Set Date Using Variable
Dim newDate As Date
newDate = #6/15/2025#
Date = newDate
Set Date Using DateSerial
Date = DateSerial(2025, 12, 25)
Set Date Using String
Date = "January 1, 2025"
Set Date Using DateAdd
Date = DateAdd("d", 30, Date)
Set Date from User Input
Dim userDate As String
userDate = InputBox("Enter new date (MM/DD/YYYY):")
If IsDate(userDate) Then
Date = userDate
Else
MsgBox "Invalid date format"
End If
Set Date with Error Handling
On Error Resume Next
Date = #1/1/2025#
If Err.Number <> 0 Then
MsgBox "Failed to set date: " & Err.Description
End If
On Error GoTo 0
Reset Date to Current System Date
' Clear any override by setting to today's real date
Date = Date
Common Errors
- Error 13: Type mismatch - occurs when the expression cannot be converted to a date.
- Error 5: Invalid procedure call or argument - occurs with invalid date values (e.g., February 30).
Best Practices
- Validate dates before assignment using
IsDate(). - Use error handling when setting dates from user input.
- Prefer
DateSerialfor programmatic date construction. - Be aware that changing the system date affects all applications.
See Also
Datefunction (retrieve current system date)Date$function (retrieve current date as string)Timestatement (set system time)DateSerialfunction (create date from components)DateValuefunction (convert string to date)Nowfunction (get current date and time)