在 Powershell 中设置像 yyyy-mm-dd 这样的默认日期格式? [英] Setup default date format like yyyy-mm-dd in Powershell?
问题描述
一个简单的 &简短的问题:
如何在 powershell 中设置 默认 日期格式,例如 yyyy-mm-dd ?所以任何日期输出都会像这种格式?
或如何在一个脚本中全局设置日期格式?
有没有办法只输出日期而没有时间?当我输出 LastWriteTime 时,默认为
13-03-2014 14:51
我只需要13-03-2014
但14:51
.
PowerShell 中的日期是一个 DateTime
对象.如果您需要特定格式的日期字符串,可以使用内置的字符串格式.
PS C:\>$date = 获取日期PS C:\>$date.ToString("yyyy-MM-dd")2014-04-02
您也可以使用字符串格式 (-f
) 运算符:
PS C:\>{0:yyyy-MM-dd}"-f $日期2014-04-02
文件的 LastWriteTime
属性也是一个 DateTime
对象,您可以使用字符串格式以任何方式输出日期的字符串表示.>
你想这样做:
Get-ChildItem -Recurse \\path\ -filter *.pdf |选择对象 LastWriteTime,Directory
您可以使用计算属性:
Get-ChildItem C:\Users\Administrator\Documents -filter *.pdf -Recurse |选择对象目录,名称,@{Name=LastWriteTime";表达式={$_.LastWriteTime.ToString("yyyy-MM-dd HH:mm")}}
运行
help Select-Object -Full
并阅读有关计算属性的更多信息.
A simple & short question:
How can I setup a default date format in powershell like yyyy-mm-dd ? so any date output will be like this format?
or How to setup a date format globally in one script ?
Is there a way to output date only without time? when I output LastWriteTime, Default is
13-03-2014 14:51
I only need 13-03-2014
but 14:51
.
A date in PowerShell is a DateTime
object. If you want a date string in a particular format, you can use the built-in string formatting.
PS C:\> $date = Get-Date
PS C:\> $date.ToString("yyyy-MM-dd")
2014-04-02
You can also use the string format (-f
) operator:
PS C:\> "{0:yyyy-MM-dd}" -f $date
2014-04-02
The LastWriteTime
property of a file is a DateTime
object also, and you can use string formatting to output a string representation of the date any way you want.
You want to do this:
Get-ChildItem -Recurse \\path\ -filter *.pdf | Select-Object LastWriteTime,Directory
You can use a calculated property:
Get-ChildItem C:\Users\Administrator\Documents -filter *.pdf -Recurse |
Select-Object Directory, Name, @{Name="LastWriteTime";
Expression={$_.LastWriteTime.ToString("yyyy-MM-dd HH:mm")}}
Run
help Select-Object -Full
and read about calculated properties for more information.
这篇关于在 Powershell 中设置像 yyyy-mm-dd 这样的默认日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!