Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with dates in PowerShell

Tags:

powershell

I am trying to use the Get-Date cmdlet to get yesterdays date. I have found the .AddDay(-1) command and that seems to work. The next thing i need to do is extract the date in YYMMDD format. This is that part i can not figure out how to do.

This is what i used to get todays date and the previous day.

 $a = Get-Date
"Day: " + $a.Day
"Month: " + $a.Month
"Year: " + $a.Year
"Hour: " + $a.Hour
"Minute: " + $a.Minute
"Second: " + $a.Second

$b=$a.AddDays(-1)
"Day: " + $b.Day
"Month: " + $b.Month
"Year: " + $b.Year
"Hour: " + $b.Hour
"Minute: " + $b.Minute
"Second: " + $b.Second
like image 948
Travis Avatar asked Mar 15 '26 14:03

Travis


1 Answers

Try this:

$b = (Get-Date).AddDays(-1).ToString("yyMMdd")
like image 141
kbrimington Avatar answered Mar 18 '26 05:03

kbrimington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!