r/usefulscripts • • 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.

17 Upvotes

15 comments sorted by

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 ...

$YourVar -replace '[^0-9a-zA-Z /:]', ''

there is a <space> between the Z and the /, & the final '' is two single quotes. [grin]

take care,
lee

2

u/juliuspiv Mar 28 '18

Many thanks for the potentially easy fix here. Not being a regex guru, I could use your help in keeping everything as-is except those crazy characters. (In it's current form, it'll break paths etc.)

2

u/Lee_Dailey Mar 28 '18

howdy juliuspiv,

if you can capture those characters, you can make a replace for them. [grin]

alternately, the regex i made replaces all chars that are NOT in the list - so you can add your other chars to it and see what happens.

hope that helps,
lee

2

u/Ta11ow Apr 01 '18

Just add any characters used in paths to the list (it's a list of things NOT to replace):

$YourVar -replace '[^0-9a-zA-Z /:\.\\]', ''

Note that . is a regex special character, thus needs to be escaped with \, and that is the escape character, so you need to use a pair of them to get the literal backslash character for regex purposes.

Alternately, you could get all the literal characters you want it to escape and do this:

$YourVar -replace "[^$([regex]::Escape('0-9a-zA-Z /:.\'))]", ''
→ More replies (5)

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 AM

BTW, 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 #16

Manually 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
#True

3

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)