Converting Weird Data Formats

by Oct 12, 2017

Sometimes, you stumble across weird data formats, i.e. in log files, that won’t autoconvert to DateTime objects. Here is a quick approach for parsing such date and time information:

$weirdDate = '03 12 --- 1988'

[DateTime]::ParseExact($weirdDate, 'MM dd --- yyyy', $null)

As you can see, ParseExact() does the job for you and wants you to specify the custom date and time format, using the standard .NET date and time characters. These are case-sensitive:

yy,yyyy: Year
M, MM, MMM, MMMM: Month
d,dd,ddd,dddd: Day
H, HH: Hour (24hr clock)
h,hh: Hour (12hr clock)
m,mm: Minute
s,ss: Second

Twitter This Tip! ReTweet this Tip!