简体   繁体   中英

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.

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.

How can this be done?

I have already tried things like (Get-Date).Day.ToString("dd") but it doesn't work.

Using ToString() and supplying date formatters (eg "yyyy" or "dd") will only work on dates. By accessing .Day or .Year , the operation is instead attempted on an integer, which will fail.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM