Converting VBScript's IsDate Function

Definition: Returns a Boolean value indicating whether an expression can be converted to a date.


Windows PowerShell makes it easy to format and manipulate date and time values … provided, of course, that you actually have date and time values. To verify that a value truly is a date-time value all you have to do is use the -is operator and check to see if the data type is datetime. For example, these two lines of code assign a value to the variable $a and then determine whether or not $a is a date-time value:

$a = 11/2/2006 
$a -is [datetime]

When you run this command you should get the following:


Why is this False; why isn’t 11/2/2006 a valid date? That’s easy: to assign a date to a variable you need to enclose that date in double quote marks and specify the [datetime] variable type:

$a = [datetime] "11/2/2006"

Without the double quote marks Windows PowerShell believes that this is a mathematical expression: 11 divided by 2 divided by 2006. In fact, if you check the value of $a you’ll get back this:


With the quotes but without the [datetime] specified, Windows PowerShell thinks this is a string and still returns False.

Return to the VBScript to Windows PowerShell home page