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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With