将字符串转换为PowerShell日期和时间格式

时间:2020-09-02 13:06:50

标签: powershell datetime

我可以使用一些帮助来弄清楚在PowerShell中将字符串转换为DateTime的格式语法。我尝试了许多组合,并继续获得 Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."

我要使用的代码如下:

$timeinfo = '1/‎3/‎2020 ‏‎9:56 AM'
$template = 'M/d/yyyy h:mm tt'
$culture = [Globalization.CultureInfo]::InvariantCulture
[DateTime]::ParseExact($timeinfo, $template, $culture)

我使用的是我在这里找到的语法:https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/parsing-custom-datetime-formats。有人愿意告诉我我做错了什么吗?我被卡住了!非常感谢!

1 个答案:

答案 0 :(得分:0)

尝试一下,让解析器完成工作:

$timeinfo = '04/02/2020 02:00 PM'

[datetime]$timeinfo