1

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!

2
  • 2
    Get-Date $date -format yyyy/M/d Commented Jun 24, 2016 at 19:27
  • 1
    alternative option just because ([datetime]$date).tostring('yyyy/MM/dd') Commented Jun 24, 2016 at 19:41

2 Answers 2

4

You can recreate an object with Get-Date:

Get-Date $date -format yyyy/M/d
Sign up to request clarification or add additional context in comments.

Comments

2

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), ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.