我从api中获取了以下格式的日期:
Mon Apr 29 14:40:17 2019
我尝试使用以下命令将其解析为有效的powershell日期:
$test = [DateTime]::ParseExact("Mon Apr 29 14:40:03 2019", "ddd MMM dd HH:mm:ss yyyy",$null)
Powershell返回"Exception valid“ParseExact with "3”参数:“字符串未被识别为有效的DateTime。”
这个问题似乎是因为工作日的缩写格式造成的。如果我去掉"Mon“和"ddd”,解析就能正常工作。
有关格式说明符的信息来自:https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#dddSpecifier
有谁知道是什么导致了这个错误吗?
发布于 2019-04-29 15:23:21
用[System.Globalization.CultureInfo]::InvariantCulture
替换$null
解决了这个问题。
工作代码是:
$test = [DateTime]::ParseExact("Mon Apr 29 14:40:03 2019", "ddd MMM dd HH:mm:ss yyyy",[System.Globalization.CultureInfo]::InvariantCulture)
https://stackoverflow.com/questions/55905609
复制相似问题