IsDate - VBScript
IsDate
This function determines whether a string represents a valid date. It is commonly used to test user input or to check whether a value is in a date format.
Syntax
IsDate(expression)
| Parameter | Description |
|—|—|
| expression | The string expression to be tested. |
Options/Flags
None
Examples
Simple usage:
If IsDate("2023-03-08") Then
Debug.Print "The string is a valid date."
End If
Complex usage:
Dim dateString As String = InputBox("Enter a date:")
If IsDate(dateString) Then
Select Case Len(dateString)
Case 10
' Format: YYYY-MM-DD
Case 8
' Format: YYYYMMDD
Case 6
' Format: YYMMDD
Case Else
' Invalid date format
End Select
Else
Debug.Print "Invalid date format."
End If
Common Issues
- Type mismatch error: Ensure that the expression provided to
IsDate
is a string. - Invalid date format: The string must be in a recognized date format (e.g., “YYYY-MM-DD”, “MM/DD/YYYY”).
Integration
IsDate
can be combined with other functions to perform more complex date operations. For example:
Dim today As Date = Date
If IsDate(InputBox("Enter a date:")) Then
Dim dateToCompare As Variant = CDate(InputBox("Enter a date:"))
If dateToCompare > today Then
Debug.Print "The entered date is in the future."
ElseIf dateToCompare < today Then
Debug.Print "The entered date is in the past."
Else
Debug.Print "The entered date is today."
End If
End If
Related Commands
- CDate
- Date
- Format
- Now