Solution: String was not recognized as a valid DateTime when calling DateTime.ParseExact on results of GetDetailsOf call

This was time suck for me, hopefully not for you after reading this.

I’m working on a PowerShell script to organize my photo library. I want to get Date Taken from EXIF data. I got the data, but then got this error:

Exception calling "ParseExact" with "3" argument(s): "String '‎1/‎1/‎2000 ‏‎12:12 AM' was not recognized as a valid DateTime."

"The error"

To the eye this looks like a valid date string, but there’s some sneaky hidden chars in there. See this:

Using: https://www.babelstone.co.uk/Unicode/whatisit.html

You can see that we have some Left to right and right to left unicode chars:

"The hidden unicode chars"

So, all you have to do is remove those nasty buggers:

$dateString = ($dir.GetDetailsOf( $file, 12 ) -replace "`u{200e}") -replace "`u{200f}"

And use the format code “g”, like so:

$date = [DateTime]::ParseExact($dateString, "g", $null)

And it works.

The code:

$dateString = ($dir.GetDetailsOf( $file, 12 ) -replace "`u{200e}") -replace "`u{200f}"
    
if ($dateString) {
    $date = [DateTime]::ParseExact($dateString, "g", $null)
}

Hopefully this saved you time and money.