You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Get-Culture DateTimeFormat在控制台不遵循本地设置,仅在已弃用ISE生效

问题:PowerShell控制台与ISE中日期格式不一致(荷兰语区域)

在配置为荷兰语的虚拟机上,执行以下代码可生成符合欧洲格式的日期时间,但仅在已弃用的PowerShell ISE中有效:

(Get-Date).toString((Get-Culture).DateTimeFormat.ShortDatePattern)
(Get-Date).toString((Get-Culture).DateTimeFormat.ShortTimePattern)

执行结果:

24/01/2025
09:19

但通过快捷方式打开PowerShell控制台运行相同代码时,格式却自动切换为美国标准:

(Get-Date).toString((Get-Culture).DateTimeFormat.ShortDatePattern)
(Get-Date).toString((Get-Culture).DateTimeFormat.ShortTimePattern)

执行结果:

1/24/2025
9:24 AM

解决方案

通过读取注册表中的系统区域设置,手动指定CurrentCulture可以解决这个问题:

$culture = (Get-ItemPropertyValue 'HKCU:\Control Panel\International' LocaleName)
[cultureinfo]::CurrentCulture = $culture
# $startTime 为 DateTime 类型变量
$startTime.ToString('d')
$startTime.ToString('t')

吐槽

这种区域格式的默认行为实在不合理——明明系统已经配置为荷兰语,PowerShell控制台却不自动继承正确的区域文化设置,非要手动调整才行。

内容的提问来源于stack exchange,提问作者Gordon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 21:05:56