我没有得到什么[DateTime] :: ParseExact()?

时间:2017-07-19 08:34:48

标签: powershell datetime exception format

$timeinfo = "01-‎06‎-2017 ‏‎12:34"
$template = "dd-MM-yyyy HH:mm"
[DateTime]::ParseExact($timeinfo, $template, $null)

结果:

Exception calling "ParseExact" with "3" argument(s): "String was not recognized
as a valid DateTime."
At line:3 char:1
+ [DateTime]::ParseExact($timeinfo, $template, $null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

我不知道这里有什么问题?当我在模板中指定它应该如何读取时,为什么我的字符串不是有效的日期时间?

2 个答案:

答案 0 :(得分:1)

您的$timeinfo变量中有一些奇怪的字符。复制和粘贴时,我得到了这个:

Strange characters

你可以通过按向右或向左箭头并通过字符串来看到这一点;它停留在奇怪的角色上。

更改为$timeinfo = "01-06-2017 12:34",您的代码按预期工作。复制并粘贴此字符串以进行测试。

修改 - 使用Unicode converter,看起来这是LRM control character

答案 1 :(得分:1)

您可能会复制并粘贴代码,因为$timedate中的字符不正确,请尝试复制并粘贴此代码:

$timeinfo = "01-06-2017 12:34"
$template = "dd-MM-yyyy HH:mm"
[DateTime]::ParseExact($timeinfo, $template, $null)
相关问题