我正在尝试解析“时间”字符串以进行比较
$Test2 =[datetime]::Parse("24/09/2015 05:41:27",[Globalization.cultureinfo]::GetCultureInfo("en-US"))
$Test2
$Test =[datetime]::Parse("23/09/2015 05:41:27",[Globalization.cultureinfo]::GetCultureInfo("en-US"))
$Test
if($Test2 -gt $Test)
write-host comparison works
我收到以下错误:
Exception calling "Parse" with "2" argument(s): "String was not recognized as
a valid DateTime."
At C:\Users\Desktop\ne.ps1:1 char:1
+ $Test =[datetime]::Parse("23/09/2015
05:41:27",[Globalization.cultureinfo]::GetC ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
最佳答案
en-US
区域性使用月-日-年格式,因此您需要:
- 转换您使用的文化
- 改变输入字符串的格式
这是一个代码示例:
### Change the date/time input string
[datetime]::Parse("09/23/2015 05:41:27")
### Use the Great Britain culture
[datetime]::Parse("23/09/2015 05:41:27", [cultureinfo]::GetCultureInfo('en-GB'))
关于powershell - 解析日期时间时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32894418/