日期时间格式在ISE和Windows窗体中的显示方式有所不同 [英] Date Time format displays differently in ISE and Windows Forms

查看:112
本文介绍了日期时间格式在ISE和Windows窗体中的显示方式有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在ISE中运行Get-Date时,会得到所需的Wednesday, 15 April 2020 12:38:03 PM.

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

但是,如果我在Windows窗体中运行相同的命令,则会以不同的格式得到04/15/2020 12:38:03.

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.

推荐答案

1.使用-Format-UFormat

自定义日期

您可以使用-Format-UFormat参数来强制设置日期的特定布局:

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"

只要您使用的是en-US区域性信息,两者都将显示所需的日期格式:

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

详细了解:

  • .NET format specifiers
  • UFormat specifiers

如果要以其他语言显示日期,则还可以强制执行某些区域性信息.请记住,-Format参数只是ToString()方法的包装.因此,您还可以使用以下行根据需要显示日期:

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')

幸运的是,该ToString()方法存在不同的重载.还有一个将文化信息作为第二个参数.因此,总而言之,您可以将不同的区域性信息传递给您的ToString()方法,以使用不同的语言获得结果:

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)

将显示:

Wednesday, 15 April 2020 08:09:24 AM

同时

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

将显示:

Mittwoch, 15 April 2020 08:09:24

3.使用预定义的区域性特定模式自定义日期

$culture.DateTimeFormat中,您还可以找到已经准备好的文化特定模式来格式化日期,并且可以使用它们而不是自己编写:

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)

将显示:

4/15/2020

同时

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

将显示:

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

这篇关于日期时间格式在ISE和Windows窗体中的显示方式有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆