SAT
SAT

Reputation: 645

How to get 3 days past date from current date Using Batch file

I am Using "%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%" to get current date using batch file

Now how can I get the date of 3 days past from today using Batch file command

o/p for "%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%" is yyyymmdd

Upvotes: 0

Views: 3791

Answers (2)

npocmaka
npocmaka

Reputation: 57272

One more way using jscript embedded into cmd script. Here's the dayAdder.bat that accepts only one argument - the days you want to add to the current date and prints the result:

  @if (@X) == (@Y) @end /* JScript comment 
    @echo off   
    cscript //E:JScript //nologo "%~f0" %*
    exit /b %errorlevel%       
@if (@X)==(@Y) @end JScript comment */

var days=parseInt(WScript.Arguments.Item(0));


Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

var date = new Date();

WScript.Echo(date.addDays(5));
WScript.Echo("Year: " + date.getFullYear());
WScript.Echo("Month: " + date.getMonth());
WScript.Echo("DayOfTeWEek: " + date.getDay());

examaple:

E:\scripts>dayAdder.bat 7
Sun Nov 8 16:27:48 UTC+0200 2020
Year: 2020
Month: 10
DayOfTeWEek: 2
DayOfTheMonth: 3

You can modify it in way that will be suitable for you.

Upvotes: 0

foxidrive
foxidrive

Reputation: 41244

This will give you a robust date 3 days into the future.

@echo off
set day=3
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "d=%yyyy%-%mm%-%dd%"

echo %d%"
pause

Upvotes: 2

Related Questions