Parsing Custom Date and Time Formats

by Feb 7, 2013

Sometimes, date and time information is not formatted according to the standards PowerShell understands by default. When this happens, you can provide a hint and tell PowerShell how to correctly interpret date and time.

In this example, $information contains date and time information in a highly unusual format:

$information = '12Nov(2012)18h30m17s'
$pattern = 'ddMMM\(yyyy\)HH\hmm\mss\s'
[datetime]::ParseExact($information, $pattern, $null) 

By providing the pattern in $pattern, PowerShell can still correctly interpret it. Note that the placeholders in $pattern are case-sensitive. "MMM" represents a short month name, whereas "mm" stands for minutes. The backslash escapes literals (text information that does not belong to the date and time format, for example braces or descriptive text).

Twitter This Tip! ReTweet this Tip!