PowerShell date format mmddyy

偶尔善良 提交于 2021-01-28 09:08:47

问题


I know I can set a date to a variable in PowerShell using

$a = Get-Date

It works great, but when looking through the documentation on formatting the date, there wasn't one for MMDDYY.

How do I accomplish this?


回答1:


Because $a is effectively a System.DateTime. You can do:

$a = Get-Date
Write-Host $a.ToString('MMddyy')

Full details on custom Date and Time Format Strings are in Custom Date and Time Format Strings.




回答2:


You just have to get creative with the -UFormat options. To get exactly what you want run this:

(Get-Date -UFormat %m)+(Get-Date -UFormat %d)+(Get-Date -UFormat %y)

However I think this is much easier to read:

Get-Date -UFormat %D



来源:https://stackoverflow.com/questions/23616614/powershell-date-format-mmddyy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!