3

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?

1
  • 1
    Have you run this on an en_US system? What was the result? Commented Apr 11, 2018 at 1:45

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Look at this article, and adapt this code to achieve what you want.

$cultures = "en-US","en-GB","fr-CA","fr-FR","ms-MY","zh-HK", "de-DE"

foreach ($c in $cultures)

{

 $culture = New-Object system.globalization.cultureinfo($c)

 $date = get-date -format ($culture.DateTimeFormat.LongTimePattern)

 New-Object psobject -Property @{"name"=$culture.displayname; "date"=$date}

}

Also be sure to look at the various properties of $culture.DateTimeFormat.

1 Comment

This might work, or some of those code. I think I will dynamically get the current culture but I just need it in that format which is a custom format
0

The $date variable is not a datetime type. Actually, this assignment will not run. I assume you meant to make it a string. Use ParseExact() to convert it to a date.

$date = [datetime]::ParseExact('10.04.2018 14:40:20','M.d.yyyy H:m:s', $null)
$NewDate = Get-Date -Date $date -Format "dd MMM yyyy h:mm:ss tt"

3 Comments

Assuming the OP just forgot quotes on $date = '10.04.2018 14:40:20', then their code does run because that string will be converted to DateTime. I suspect the issue is with the de-DE culture and interop there.
my issue is really that in the US April 10 would be 4/10/2018 and in Germany it would be 10/4/2018. I can't use parseExact because I don't know the exact format that it will be in at all times.
If you do not know the incoming data, I do not know what can be done. Selecting a culture, as @KoryGill suggested, will still need to be done on the basis of the incoming data format. Will the incoming data be in en_US format when the program is run on a US machine and in de_DE format when run on a German machine?

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.