0

I'm trying to implement a PowerShell script to compare DateTime from certificate file(Jave Keystore).

However, the DateTime format that I extract from keystore file is quite complex as example below.

Mon Mar 13 06:40:26 CDT 2023

Sat Sep 18 20:41:47 CDT 2027

It includes time and timezone in the String but I actually need only date like 13-Mar-2023.

Could anyone help suggest how I return this String to be DateTime for comparison? Thanks a lot.

Mek
  • 35
  • 6

1 Answers1

2

You can use the [datetime]::ParseExact() method for this:

$dateString = 'Mon Mar 13 06:40:26 CDT 2023'

$date = [datetime]::ParseExact($dateString, 'ddd MMM dd HH:mm:ss CDT yyyy', [cultureinfo]'en-US')
$date.ToString('dd-MMM-yyyy')

Result:

13-Mar-2023

CDT means Central Time Zone (UTC - 6), switched to DayLight Saving Time --> Central Daylight Time (CDT) which is five hours behind UTC

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Hello Theo, it's very clear and helpful for me. I really appreciate for your help :) – Mek Jul 23 '20 at 09:03