问题
$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