If you need to test whether some information resembles a valid date format, here is a test function:- function Test-Date
- {
- param
- (
- [Parameter(Mandatory=$true]
- $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.
http://powershell.com/cs/blogs/tips/archive/2014/02/04/testing-for-valid-date.aspx |