Parsing Custom Date and Time Formats

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

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!


Posted Feb 07 2013, 06:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.