简体   繁体   中英

Convert Date variable into particular format in Powershell

Get-Date -format yyyy/M/d works in PS1 but it doesnt work with variable date like, $date -format yyyy/M/d. It prompts an error - Unexpected token in expression or statement.

How can I make the $date variable work.

Thanks, A curious mind!

您可以使用Get-Date重新创建对象:

Get-Date $date -format yyyy/M/d

You have to use ($date).ToString('yyyy/M/d')

Editing to say you can pipe the $date variable to Get-Member in order to reveal the available methods. Then we can search for formatting a DateTime string in PowerShell For example, this question

$date | GM
TypeName: System.DateTime
ToString             Method         
 string ToString(), string ToString(string format), 
 string ToString(System.IFormatProvider provider), ...

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