2

I want yesterday's date as "MMdd", like "0418" today's date is "0419".

I tried this:

$d = (Get-Date).AddDays(-1) | select -Property Month,Day
$e = $d.Day

$d = $d.Month

$total = "$d" + "$e"

Write-Host $total

But the output was "418".

As you can see, this is a three-letter string, but I want a four-letter one. Because I want to search files they have that format and created a day before.

1 Answer 1

3

The date object has a method ToString, which allows you to specify the format:

$d = (Get-Date).AddDays(-1);
$d.ToString("MMdd");

I am calling this on April the 20th, and the output is

0419

See this link for a description of the available date format strings.

Sign up to request clarification or add additional context in comments.

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.