Testing for Valid Date

by Feb 4, 2014

If you need to test whether some information resembles a valid date format, here is a test function:

function Test-Date
    (($Date -as [DateTime]) -ne $null)

It uses the -as operator to try and convert the input to a DateTime format. If this fails, the result is $null, so the function checks for this and returns $true or $false. Note that the -as operator uses your local DateTime format.

Twitter This Tip! ReTweet this Tip!