使用PowerShell将月份数转换为月份名称

Pmi*_*e86 15 powershell datetime

我需要转换"08"来显示"八月"

我无法使用,Get-Date因为这来自fileName,它可能会也可能不会改变.

因此,目前我将名称拆分为数组,访问我需要的数据,并需要将mm部分转换为MMMM

01 = January
03 = March
etc
Run Code Online (Sandbox Code Playgroud)

Roh*_*rds 19

全月名称:

(Get-Culture).DateTimeFormat.GetMonthName(8)
Run Code Online (Sandbox Code Playgroud)

简写:

(Get-Culture).DateTimeFormat.GetAbbreviatedMonthName(8)
Run Code Online (Sandbox Code Playgroud)

  • 如果你希望代码在不同的计算机上工作相同,我建议使用`[cultureinfo] :: InvariantCulture`而不是`Get-Culture`. (2认同)
  • @bouvirr 提出了一个很好的观点。Get-Culture 将获取运行该命令的计算机的当前文化,因此 8 在文化设置为西班牙语的计算机上将被格式化为“agosto”。如果您希望它始终返回“August”(无论文化是什么),请将 (Get-Culture) 更改为 [cultureinfo]::InvariantCulture。 (2认同)

C. *_*alt 7

本着使用最简单的方式的精神:

Get-Date -UFormat %B

或者获取月份的缩写:

Get-Date -UFormat %b

这也可以很好地从 BASH 和其他使用这种日期格式的脚本语言移植。


小智 6

(Get-Culture).DateTimeFormat.GetMonthName((Get-Date).Month)
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您正在查找当前月份,请尝试以下操作:

$currentMonth = Get-Date -UFormat %m

$currentMonth = (Get-Culture).DateTimeFormat.GetMonthName($currentMonth)
Run Code Online (Sandbox Code Playgroud)