Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
949 views
in Technique[技术] by (71.8m points)

powershell - Date Time format displays differently in ISE and Windows Forms

When I run Get-Date in ISE, I get Wednesday, 15 April 2020 12:38:03 PM which I want.

However, if I run the same command in Windows Forms, I get 04/15/2020 12:38:03 in a different format.

I run them from the same computer so it must be the same cultural/region.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1. Customizing your date using -Format or -UFormat

You can use the -Format or the -UFormat paramater to enforce a certain layout of your date:

Get-Date -Format "dddd, d MMMM yyyy hh:mm:ss tt"
Get-Date -UFormat "%A, %e %B %Y %r"

Both will display your desired date format, as long as you are using en-US culture information:

Wednesday, 15 April 2020 08:09:24 AM

Learn more about:

2. Customizing your date with different culture information

If you want to display the date in a different language, you can also enforce a certain culture information. Keep in mind that the -Format parameter is just a wrapper for the ToString() method. So you can also use the following line to display your date as desired:

(Get-Date).ToString('dddd, d MMMM yyyy hh:mm:ss tt')

Fortunately, there exist different overloads of that ToString() method. There is also one, that takes culture information as a second parameter. So in conclusion you can pass different culture info to your ToString() method to get results in different languages:

$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('en-US')
(Get-Date).ToString('dddd, d MMMM yyyy hh:mm:ss tt', $culture)

will display:

Wednesday, 15 April 2020 08:09:24 AM

and at the same time

$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('de-DE')
(Get-Date).ToString('dddd, d MMMM yyyy hh:mm:ss tt', $culture)

will display:

Mittwoch, 15 April 2020 08:09:24

3. Customizing your date with predefined culture specific patterns

In $culture.DateTimeFormat you can also find already prepared culture specific patterns to format your date and you can use them instead of writing them on your own:

$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('en-US')
(Get-Date).ToString($culture.DateTimeFormat.ShortDatePattern, $culture)

will display:

4/15/2020

and at the same time

$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('de-DE')
(Get-Date).ToString($culture.DateTimeFormat.ShortDatePattern, $culture)

will display:

15.04.2020

Btw: A similar pattern to yours, specified in your question, would be:

$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('en-US')
(Get-Date).ToString($culture.DateTimeFormat.FullDateTimePattern, $culture)

Wednesday, April 15, 2020 8:09:24 AM


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...