标题: [转载代码] [PowerShell每日技巧]判断日期是否合法(20140204) [打印本页]
作者: DAIC 时间: 2014-2-17 10:36 标题: [PowerShell每日技巧]判断日期是否合法(20140204)
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
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |