0

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))

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

3 Comments

This only works for english systems. When you have another you may replace $null with [system.globalization.cultureinfo]("en-US")
@Olaf thx for the comment. Adding that worked
;-) ... You're welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.