1
votes
$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?

2

2 Answers

2
votes

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

Strange characters

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

1
votes

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)