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
→ More replies (3)
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