Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Get-Date not formatting properly

I have a date I am reading from somewhere so I am retrieving as a string value. My Culture is de-DE but need the time to have the AM/PM for when the script runs in en-US:

$date = 10.04.2018 14:40:20
$NewDate = Get-Date -Date $date -Format "dd MMM yyyy h:mm:ss tt"

What I want is $NewDate to be 10 Apr 2018 2:40:20 PM but am only able to get 10 Apr 2018 2:40:20. In english the tt translates to an AM/PM just fine, but how do I get it here?

like image 703
vee Avatar asked Aug 14 '26 19:08

vee


1 Answers

If you want to rely on a certain date string, no matter where and with what culture information your script is run, you have to define a fixed culture info for your output:

$date = '10.04.2018 14:40:20'
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('en-US')
(Get-Date $date).ToString('dd MMM yyyy h:mm:ss tt', $culture)

Output:

10 Apr 2018 2:40:20 PM

The output will always be the same, even on your 'de-DE' machine. You can read more about date formatting in my answer here.

like image 167
stackprotector Avatar answered Aug 16 '26 21:08

stackprotector



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!