What am I not getting about [DateTime]::ParseExact()?

爱⌒轻易说出口 提交于 2021-01-28 05:02:05

问题


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

results in:

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

I can't tell what is wrong here? Why is my string not a valid datetime when I specify in the template how it should read it?


回答1:


You have some strange characters in your $timeinfo variable. When copying and pasting, I am getting this:

You can see this by pressing the right or left arrow and going through the string; it pauses at the odd character.

Changing to $timeinfo = "01-06-2017 12:34", your code works as expected. Copy and paste this string to test.

Edit - Using a Unicode converter, it looks like this is LRM control character




回答2:


You probably copy and paste the code because there are incorrect characters in the $timedate, try copy and paste this:

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


来源:https://stackoverflow.com/questions/45185145/what-am-i-not-getting-about-datetimeparseexact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!