Setup default date format like yyyy-mm-dd in Powershell?

后端 未结 3 1696
借酒劲吻你
借酒劲吻你 2020-12-16 01:44

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?

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 02:25

    A date in PowerShell is a DateTime object. If you want a date string in a particular format, just use the built-in string formatting.

    PS C:\> $date = get-date
    PS C:\> $date.ToString("yyyy-MM-dd")
    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:

    gci -recu \\path\ -filter *.pdf | select LastWriteTime,Directory
    

    You can use a calculated property:

    get-childitem C:\Users\Administrator\Documents -filter *.pdf -recurse |
      select 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.

提交回复
热议问题