Parse an Exact Date

by Mar 17, 2016

If you need to parse a date or time information out of raw text, and if the date and time format does not adhere to the standards of your operating system, then you can use a template and define the date and time pattern yourself. Have a look

#requires -Version 1
Get-Date -Format 'yyyy-MM-dd HH:mm:ss fffff'

$date   = '1990 --03-- 29'
$pattern = 'yyyy --MM-- dd'

[DateTime]::ParseExact($date, $pattern, $null)

The pattern uses the default .NET placeholders for date and time (which are case sensitive). You can see the most frequently used placeholders in the initial Get-Date statement.

$date contains the date information we are after. $pattern describes the date pattern used in that string. ParseExact() does the heavy lifting and returns a DateTime object.

Twitter This Tip! ReTweet this Tip!