GhostJumper
GhostJumper

Reputation: 111

Powershell parse DateTime

I want to parse this String I get from OpenSSL to DateTime:

Dec 23 03:54:47 2021


I tried the following things without success:

([datetime]::ParseExact($datestring, "%b %H:%M:%S %Y", $null))
([datetime]::ParseExact($datestring, "bbb HH:MM:SS YYYY", $null))

Upvotes: 0

Views: 431

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

The format string you want is MMM dd HH:mm:ss yyyy:

PS ~> [datetime]::ParseExact('Dec 23 03:54:47 2021', 'MMM dd HH:mm:ss yyyy', $null)

Thursday, December 23, 2021 3:54:47 AM

As Olaf mentions, the month name specifiers will only work on English month names when the current locale is English.

If you want to always parse English month names regardless of OS localization settings, explicitly pass an en-US Culture object for the 3rd method parameter:

$targetCulture = [cultureinfo]::new('en-US')
[datetime]::ParseExact('Dec 23 03:54:47 2021', 'MMM dd HH:mm:ss yyyy', $targetCulture)

Upvotes: 2

Related Questions