使用powershell获取修改为AM/PM格式的日期

NoS*_*otz 5 powershell

我目前有

$GAIBC = Get-Item "C:\Users\nosho\Documents\Paradox Interactive\Stellaris\mod\gaibc\common\districts\02_rural_districts.txt" | Foreach {$_.LastWriteTime}
Run Code Online (Sandbox Code Playgroud)

哪个输出像这样

12/18/2018 16:54:32
Run Code Online (Sandbox Code Playgroud)

但我希望它像这样输出

12/18/2018 4:54 PM
Run Code Online (Sandbox Code Playgroud)

有什么办法可以做到吗?

小智 8

使用 DateTime 对象格式。更多信息在这里

$GAIBC = Get-Item "C:\Users\nosho\Documents\Paradox Interactive\Stellaris\mod\gaibc\common\districts\02_rural_districts.txt" | Foreach {$_.LastWriteTime.ToString("MM/dd/yyyy hh:mm:ss tt")}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,即使是下午 4 点而不是凌晨 4 点,“hh”也会返回“04”。使用一个“h”返回不带前导零的时间(对于这些小时,它仍然会执行 10、11、12)。 (2认同)