2

Here's my function:

Function Convert-StringDateTime {

<#
 Function extracts datetime object from a specificlly
 formatted string containing a date time in the format:
 M/dd/yyy h:mm tt
 Ex: 8/13/2010 1:17 AM
#>


  Param (
    [Parameter(Mandatory=$True)]
      [String] $FunctDteStr
  )

  $DateFmt = "M/dd/yyyy"
  $TimeFmt = "h:mm tt"
  
  $DTParts = $FunctDteStr -split(" ")
  $DateStr = $DTParts[0]
  $TimeStr = $DTParts[1] + " " + $DTParts[2]
  $DT = [DateTime]::ParseExact($DateStr,$DateFmt,$null)
  $TM = [DateTime]::ParseExact($TimeStr,$TimeFmt,$null)
  
  $CreationDateTime = $DT + $TM.TimeofDay
  
  Return ,$CreationDateTime

} #End Function Convert-StringDateTime

If I call it like this:

$CR = Convert-StringDateTime -FunctDteStr "8/13/2010 1:17 PM"

It runs fine and returns $CR as a datetime value.

PS> $CR

Friday, August 13, 2010 1:17:00 PM


However, if I call it using a variable which contains the same string...

PS>  $($MetaData[0].'Date Taken')
‎8/‎13/‎2010 ‏‎1:17 AM

PS> $CR = Convert-StringDateTime -FunctDteStr $($MetaData[0].'Date Taken')
Exception calling "ParseExact" with "3" argument(s): "String was not 
recognized as a valid DateTime."
At G:\BEKDocs\Scripts\Functions\Function - Convert-StringDateTime.ps1:22 char:3
+   $DT = [DateTime]::ParseExact($DateStr,$DateFmt,$null)
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException
 
Exception calling "ParseExact" with "3" argument(s): "String was not 
recognized as a valid DateTime."
At G:\BEKDocs\Scripts\Functions\Function - Convert-StringDateTime.ps1:23 char:3
+   $TM = [DateTime]::ParseExact($TimeStr,$TimeFmt,$null)
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException
 
The property 'TimeofDay' cannot be found on this object. Verify that the 
property exists.
At G:\BEKDocs\Scripts\Functions\Function - Convert-StringDateTime.ps1:25 char:3
+   $CreationDateTime = $DT + $TM.TimeofDay
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

It will also produce the errors if I use this syntax:

$CR = Convert-StringDateTime -FunctDteStr "$($MetaData[0].'Date Taken')"

Just for completeness:

PS>  $($MetaData[0].'Date Taken') | gm

   TypeName: System.String

I'm at a total loss...

RetiredGeek
  • 2,980
  • 1
  • 7
  • 21
  • In short: EXIF data embedded in image files contains date strings that unexpectedly have invisible control characters; the linked duplicate shows how to remove them. – mklement0 Aug 24 '22 at 22:14
  • Glad to hear it; I've closed this post as a duplicate, along with a short comment summarizing the problem. – mklement0 Aug 24 '22 at 22:16
  • The function may fail with a non-english locale, like `de-DE` (it does on my system). Replace the 3rd (`$null`) argument of `ParseExact` by `[CultureInfo]::InvariantCulture` to do locale-agnostic DateTime parsing. – zett42 Aug 24 '22 at 22:18
  • 1
    Good point, @zett42. Given that PowerShell's _casts_ invariably use the invariant culture and given that the format is recognized _by default_ there, `[datetime] '8/13/2010 1:17 AM'` will do, for instance. – mklement0 Aug 24 '22 at 22:51

0 Answers0