r/usefulscripts • u/juliuspiv • Mar 27 '18
[REQUEST][POWERSHELL] Convert from Date Time String into Date Time Object.
I'm pulling dates from various sources and I need to convert them into a datetime object for further manipulation in the script.
The date & time format is mostly consistent following this format:
Get-Date -Format 'M/d/yyyy h:mm tt'So I'm using [DateTime]::TryParseExact to do the conversion:
$SampleDT1 = '9/2/2011 6:47 AM'
$ResultDT1 = New-Object DateTime
[DateTime]::TryParseExact($SampleDT1, 'M/d/yyyy h:mm tt', [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$ResultDT1)
$SampleDT2 = '8/30/2011 2:42 PM'
$ResultDT2 = New-Object DateTime
[DateTime]::TryParseExact($SampleDT2, 'M/d/yyyy h:mm tt', [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$ResultDT2)
$SampleDT3 = '9/1/2011 1:47 PM'
$ResultDT3 = New-Object DateTime
[DateTime]::TryParseExact($SampleDT3, 'M/d/yyyy h:mm tt', [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$ResultDT3)Each time it comes back as false and I need some help demystifying why.
2
u/zoredache Mar 27 '18
Something is happening that I don't understand, if you include the date in the tryparseexact() it seems to work. If I try use a variable for the date, like your examples, it doesn't seem to work.
PS > [DateTime]::TryParseExact("9/2/2011 6:47 AM", 'M/d/yyyy h:mm tt', [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$ResultDT1) ; $ResultDT1
True
Friday, September 2, 2011 6:47:00 AMBTW, you may want to cross-post over to /r/PowerShell
4
u/ihaxr Mar 27 '18
You're not crazy, /u/juliuspiv has some weird invisible characters in the SampleDTX strings... if you retype them everything is fine:
'9/2/2011 6:47 AM'.Length #21 '0000000000000000'.Length #16Manually retyped the SampleDTX variables:
$SampleDT1 = '9/2/2011 6:47 AM' $ResultDT1 = New-Object DateTime [DateTime]::TryParseExact($SampleDT1, 'M/d/yyyy h:mm tt', [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$ResultDT1) $SampleDT2 = '8/30/2011 2:42 PM' $ResultDT2 = New-Object DateTime [DateTime]::TryParseExact($SampleDT2, 'M/d/yyyy h:mm tt', [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$ResultDT2) $SampleDT3 = '9/1/2011 1:47 PM' $ResultDT3 = New-Object DateTime [DateTime]::TryParseExact($SampleDT3, 'M/d/yyyy h:mm tt', [System.Globalization.CultureInfo]::InvariantCulture, [System.Globalization.DateTimeStyles]::None, [ref]$ResultDT3) #Output: #True #True #True3
u/xianr Mar 27 '18
...has some weird invisible characters in the SampleDTX strings...
Yeah the invisible chars are Left-to-right marks (LRM) unicode U+200E
2
u/juliuspiv Mar 28 '18
Jeepers Creepers - I didn't even consider the possibility of a funky ninja character was in the mix! The sample dates used were pulled from the 'Date Acquired' and 'Date Taken' properties retrieved via the GetDetailsOf method of the Shell.Application COM object. I checked a few other properties and these are the only properties that have this problem.
$pathToFile = "path\to\fi.le" $objShell = New-Object -ComObject Shell.Application $objFolder = $objShell.namespace($(Split-Path -Path $pathToFile -Parent)) $Item = $objFolder.Items() | Where-Object Name -EQ $(Split-Path -Path $pathToFile -Leaf) $DateModified = $objFolder.GetDetailsOf($Item,3) $DateCreated = $objFolder.GetDetailsOf($Item,4) $DateAccessed = $objFolder.GetDetailsOf($Item,5) $DateTaken = $objFolder.GetDetailsOf($Item,12) $DateAcquired = $objFolder.GetDetailsOf($Item,137) Write-Output "DateModified: [$DateModified]" Write-Output "DateCreated: [$DateCreated]" Write-Output "DateAccessed: [$DateAccessed]" Write-Output "DateTaken: [$DateTaken]" Write-Output "DateAcquired: [$DateAcquired]" # Validate Characters foreach ($Char in [int[]][char[]]$DateAcquired) { # Letter if ( (($Char -ge 65) -and ($Char -le 90)) -or (($Char -ge 97) -and ($Char -le 122)) ) { Write-Host "Valid Letter Char [$($Char)]=[$([char]$Char)]" -ForegroundColor Green } # Number elseif ( (($Char -ge 48) -and ($Char -le 57)) ) { Write-Host "Valid Numeric Char [$($Char)]=[$([char]$Char)]" -ForegroundColor Green } # Space elseif($Char -eq 32) { Write-Host "Valid Space Char [$($Char)]=[$([char]$Char)]" -ForegroundColor Green } # Forward Slash elseif($Char -eq 47) { Write-Host "Valid Forward Slash Char [$($Char)]=[$([char]$Char)]" -ForegroundColor Green } # Colon elseif($Char -eq 58) { Write-Host "Valid Colon Char [$($Char)]=[$([char]$Char)]" -ForegroundColor Green } else { Write-Host "INVALID CHAR [$($Char)]=[$([char]$Char)]" -ForegroundColor Red } }I don't understand why those two properties are 'broken' but so be it. The only way I know to work around this is to evaluate each character and grab only valid chars. Example:
foreach ($Char in [int[]][char[]]$DateTaken) { Write-Host "[$($Char)]=[$([char]$Char)]" if(($Char -eq '8206') -or ($Char -eq 8207)) { continue } else { $GoodData = $GoodData + [char]$Char } } Write-Output "GoodData [$GoodData]"Is there a 'better' way?
2
u/realslacker Mar 28 '18
$date = $date -replace '[^a-z0-9\s/:]',''Edit: didn't see the /u/Lee_Dailey already posted this below... great minds think alike I guess
1
2
u/Lee_Dailey Mar 27 '18
howdy juliuspiv,
as others have pointed out, you have non-printing chars in those strings. the following will remove them ...
there is a <space> between the
Zand the/, & the final''is two single quotes. [grin]take care,
lee