CDate - VBScript


Overview

The CDate command in VB Script converts a string representing a date and time expression into a Date object. This is particularly useful when working with dates stored in a textual format or when you need to perform date calculations and comparisons.

Syntax

CDate(Expression)
  • Expression: A string representing the date and time expression to be converted.

Options/Flags

N/A

Examples

Example 1: Converting a String to a Date Object

Dim myDate
myDate = CDate("2023-03-08")

Example 2: Comparing Date Objects

Dim date1, date2
date1 = CDate("2023-03-08")
date2 = CDate("2023-03-10")

If date1 < date2 Then
  ' date1 is earlier than date2
End If

Common Issues

  • Date Format Errors: Ensure that the input string matches the expected date format; otherwise, the conversion may fail.
  • Time Zone Considerations: The CDate function assumes the local time zone. If the input string represents a time in a different time zone, you may need to adjust it accordingly.

Integration

The CDate command can be integrated with other VB Script commands, such as FormatDateTime, to convert dates into different formats or perform date calculations using the DateAdd and DateDiff functions.

  • Date
  • DateAdd
  • DateDiff
  • FormatDateTime