首页 > 解决方案 > 将字符串转换为 PowerShell 日期和时间格式

问题描述

我可以使用一些帮助来确定在 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 。有人愿意告诉我我做错了什么吗?我被困住了!非常感谢!

标签: powershelldatetime

解决方案


试试这个,让解析器完成工作:

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

[datetime]$timeinfo

推荐阅读