Share via


Parsing Dates by Hand

I’m always impressed by the amount of variety in input formats that Get-Date can accept and still parse it into a date.

However, there is one time that it vexes me: US vs. EU date strings.  Specifically, the US uses dd/MM/[yy]yy, while the EU uses MM/dd/[yy]yy.  In my mind, neither make sense; it should be yyyy-MM-dd, so it’s always sortable, but that’s another matter.

For some dates, such as today, 3/13, it’s pretty obvious I mean March 13th, and not … uh, what’s the 13th month again?

Exactly.

And, for days such as 1/1, 2/2, and such, that doesn’t matter very much. 

Great, that solves a dozen days out of the year.  The word ‘non-scalable’ was made for ‘solutions’ like that.

 Enter [DateTime]::ParseExact().;
$string = ‘03/12/14’;
EU: $DateTime = [DateTime]::ParseExact($string, ‘dd/MM/yy’, [CultureInfo]::InvariantCulture); # December 3rd.US: $DateTime = [DateTime]::ParseExact($string, ‘MM/dd/yy’, [CultureInfo]::InvariantCulture); # yesterday, not December!