-2

Im trying to add 1 to the current day but for that to be possible i have get the date to single digit(because it cant add 01 to 1 or something like that). After that works, i cant get it back to double digit (ex: 01, 02, 09) My code looks like this:

Set /a day=%date:~1,2% (this is 2)
Set /a day+=1            (this is 3 now)
Set day=0%day%       (this is zero)     

This only says the 0 in front I tried with two different variables but it works same

  • 2
    I think I have posted a date math batch file solution a dozen times on StackOverFlow. Did you search? Another option is to use Powershell which makes date calculations effortless with much less code. – Squashman May 02 '18 at 02:22
  • Its the simplest solution i got for this problem. I searched how to make sentences with variables for adding the zero, the code seems as it should work but doesnt – Marian Diam May 02 '18 at 02:25
  • What happens when you add one to February 28? – Squashman May 02 '18 at 02:27
  • Those are the times i manually do it – Marian Diam May 02 '18 at 03:53
  • 2
    @MarianDiam By the way: The wrong result of your three lines is caused by embedding them in a command block starting with `(` and ending with matching `)` with `Day` defined/modified inside the command block and don't use [delayed expansion](https://ss64.com/nt/delayedexpansion.html). Questions caused by not knowing about how Windows command processor preprocesses environment variable references before execution of a command line or an entire command block and how to use delayed environment variable expansion are asked daily on Stack Overflow. Run `set /?` and read all help pages. – Mofi May 02 '18 at 09:11
  • 1
    The command line `set /A day=%date:~1,2%` is to remove the leading space, right? why not using `set /A day=%date: =%`, so two-digit numbers are also handled correctly? By the way, I bet your code fragment is placed within a parenthesised block of code, so `day` becomes expanded to the value it has got when the whole block is parsed, because you lack using [delayed expansion](http://ss64.com/nt/delayedexpansion.html)... – aschipfl May 02 '18 at 11:46
  • How to calculate tomorrows date was posted several times on Stack Overflow as Squashman wrote. Two examples can be seen on [Windows batch event reminder for next day](https://stackoverflow.com/questions/37532952/). – Mofi May 03 '18 at 15:18

2 Answers2

1

By changing the number in this variable day=+1 to will add more days to your result, or changing to day=-1 will remove a day etc..

@echo off
set day=+1
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 "result=%yyyy%-%mm%-%dd%"
echo %DD%
pause

This will only echo the day +1. If however you want to echo the full date result, simply do echo %result%

YYYY, MM and DD have been split into single variables anyway so you can use it if you like.

Just as a sidenote:

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

is pretty much the same as:

echo s=DateAdd("d",%day%,now) : d=weekday(s) >"%temp%\%~n0.vbs" 
echo WScript.Echo year(s)^& right(100+month(s),2)^& right >>"%temp%\%~n0.vbs"

CMD will pretty much do the same for both and that is to pipe the output to file. I simply do this so by reading the line, you can immediately see what the line does, echoing to external file, the string that follows.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

As @Squashman suggested, PowerShell makes this pretty easy.

FOR /F %a IN ('powershell -NoProfile -Command "'{0:yyyy-MM-dd}' -f (Get-Date).AddDays(1)"') DO SET "NEWDATE=%a"
ECHO %NEWDATE%

In a .bat script, double the variable % characters.

FOR /F %%a IN ('powershell -NoProfile -Command "'{0:yyyy-MM-dd}' -f (Get-Date).AddDays(1)"') DO SET "NEWDATE=%%a"
ECHO %NEWDATE%

It is even easier if you use PowerShell and not cmd where it must be wrapped in a FOR loop.

$newdate = '{0:yyyy-MM-dd}' -f (Get-Date).AddDays(1)
Mofi
  • 46,139
  • 17
  • 80
  • 143
lit
  • 14,456
  • 10
  • 65
  • 119
  • Im not too good with cmd and worse with powetshell but your code solution sure works! – Marian Diam May 05 '18 at 17:36
  • The "loop" runs over each output line from the command. This command only outputs one (1) line. The first field from the line, `%%a`, is placed into the NEWDATE variable. This works as designed by Microsoft. – lit May 05 '18 at 17:52