[英]How to format Get-Date Day as 2 digits in Powershell
I try to get the Day as a two digit number out of Get-Date in PowerShell.我尝试从 PowerShell 中的 Get-Date 中获取两位数的日期。
When I try (Get-Date).Day
the result will be for example 6 but I want to have it as 06. Also for the month.当我尝试
(Get-Date).Day
,结果将是例如 6,但我想将其设为 06。也是当月。
How can this be done?如何才能做到这一点?
I have already tried things like (Get-Date).Day.ToString("dd")
but it doesn't work.我已经尝试过
(Get-Date).Day.ToString("dd")
但它不起作用。
Using ToString()
and supplying date formatters (eg "yyyy" or "dd") will only work on dates.使用
ToString()
并提供日期格式化程序(例如“yyyy”或“dd”)仅适用于日期。 By accessing .Day
or .Year
, the operation is instead attempted on an integer, which will fail.通过访问
.Day
或.Year
,该操作将改为尝试对整数进行操作,这将失败。
Try (for the day):尝试(当天):
(Get-Date).ToString("dd")
...and (and for the month): ...和(以及本月):
(Get-Date).ToString("MM")
See here for custom formatting of dates using ToString()
请参阅此处使用
ToString()
自定义日期格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.