Jump to content

Recommended Posts

Posted (edited)

I am wondering if there are any resident batch file experts on here.. I am looking to run some reports in commandreporter and want to set the end date of the report to "one week from today". I would appreciate any help!

 

SIMS_Dash_B.txt

Edited by bfelton
Posted

If you add the following code to your batch file it will calculate what the date is one week from today (via PowerShell) and store it in the 'TodayPlusOneWeek' variable.

 

FOR /F %%i IN ('powershell ^(Get-Date^).AddDays^(+7^).ToString^('dd-MM-yyyy'^)') DO (
   SET TodayPlusOneWeek=%%i
)

ECHO %TodayPlusOneWeek%

  • Thanks 1
Posted
If you add the following code to your batch file it will calculate what the date is one week from today (via PowerShell) and store it in the 'TodayPlusOneWeek' variable.

 

FOR /F %%i IN ('powershell ^(Get-Date^).AddDays^(+7^).ToString^('dd-MM-yyyy'^)') DO (
   SET TodayPlusOneWeek=%%i
)

ECHO %TodayPlusOneWeek%

 

Will this take into account different length months and leap years?

Posted
Will this take into account different length months and leap years?

Good question! :)

 

If you use PowerShell to add 7 days onto 22/02/2020, it will return 29/02/2020 and not 01/03/2020.

 

$Date = Get-Date -Year 2020 -Month 02 -Day 22
$Date.AddDays(+7).ToString('dd-MM-yyyy')

 

QyJcmq.png

 

Do the same calculation with 22/02/2021 and you will get 01/03/2021.

 

JfXpJD.png

 

tl;dr. If Windows can handle different length months and leap years, PowerShell will too.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...