Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append +1 to date in batch file

I have a batch file which creates today's date just fine. Now I need to update it to show tomorrow's date. Any help is much appreciated:

@echo off
set TimeStamp=12:00:00


FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B

FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B

FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B

FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B

SET date="%yyyy%-%mm%-%dd% %TimeStamp%"

echo %date%
like image 328
YoungDev Avatar asked Dec 26 '22 22:12

YoungDev


1 Answers

The 'problem' is that you need to think about February, leap-years, etc.

Paul Tomasi has posted a rather brilliant script on his site where he explains it completely, even including a flowchart.

::================================================
:: TOMORROW.BAT - Written by Paul Tomasi (c)2010
::
:: Function to return tomorrow's date
::================================================
@echo off

set /a d=%date:~0,2%
set /a m=%date:~3,2%
set /a y=%date:~6,4%

:loop
   set /a d+=1

   if %d% gtr 31 (
      set d=1
      set /a m+=1

      if %m% gtr 12 (
         set m=1
         set /a y+=1
      )
   )

xcopy /d:%m%-%d%-%y% /h /l "%~f0" "%~f0\" >nul 2>&1 || goto loop

echo %d%/%m%/%y%

::------------------------------------------------

flowchart batch time-calculation by Paul Tomasi

So it's either this, or diving into hybrid batch-scripts.

Hope this helps!

like image 128
GitaarLAB Avatar answered Dec 29 '22 10:12

GitaarLAB