4

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%
rene
  • 41,474
  • 78
  • 114
  • 152
YoungDev
  • 41
  • 1
  • 1
  • 2

4 Answers4

6

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!

GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
3
@echo off &SETLOCAL
SET "today=%date%"
ECHO(today is       %today%
CALL:DateToJDN %today% todayNo
ECHO(this is day no.        %todayNo%
SET /a tomorrowNo=todayNo+1
ECHO(tomorrow is day no.    %tomorrowNo%
CALL:JDNToDate %tomorrowNo% tomorrow
ECHO(tomorrow is        %tomorrow%
GOTO:EOF

rem Convert the date to Julian Day Number

:DateToJDN dd.mm.yyyy jdn=
setlocal
set date=%1
set /A yy=%date:~-4%, mm=1%date:~-7,2% %% 100, dd=1%date:~-10,2% %% 100
set /A a=mm-14, jdn=(1461*(yy+4800+a/12))/4+(367*(mm-2-12*(a/12)))/12-(3*((yy+4900+a/12)/100))/4+dd-32075
endlocal & set %2=%jdn%
exit /B

rem Convert Julian Day Number back to date

:JDNToDate jdn dd.mm.yyyy=
setlocal
set /A l=%1+68569,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447,dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yy=100*(n-49)+i+l
if %dd% lss 10 set dd=0%dd%
if %mm% lss 10 set mm=0%mm%
endlocal & set %2=%dd%.%mm%.%yy%
exit /B

endlocal

today is                08.08.2013
this is day no.         2456513
tomorrow is day no.     2456514
tomorrow is             09.08.2013
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • Hi Endoro, I'm getting the following error while running your code example: `code` ** today is Thu 08/08/2013 Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021). this is day no. tomorrow is day no. 1 tomorrow is 25.11.-4713** – YoungDev Aug 08 '13 at 19:05
  • @user2665516 The `date` format depends on local settings (your country settings). My date format is `dd.mm.yyyy`, as you can see in the script's output part. – Endoro Aug 08 '13 at 19:11
  • None of these options worked for me and using PowerShell was not an option. What I ended up doing was to use VBScript which created the date in format I wanted. Then I also created the .batch file on the fly. Since my account is new it won't let me answer my question. I will try to post the solution later. – YoungDev Aug 08 '13 at 22:24
  • @YoungDev you can adjust my code for your date format. I could make it even if I knew it. – Endoro Aug 08 '13 at 22:44
  • I'm a little nervous that this doesn't perfectly implement the Julian Date conversion. I'll give it a shot and be back if things fail over Christmas / Leap Years ;) – Kieveli Sep 14 '16 at 15:57
  • I had to edit it a little to get it to work, but it's close. Not sure where to put my version. The parsing out of the date wasn't working, so I changed it to not use negatives (also changed the date format to yyyymmdd for what I was working on) – Kieveli Sep 15 '16 at 13:00
1

Sorry, I can't tell what you're doing with your Batch code. Can you comment this? Why do you need the timestamp of 12:00?

In my german version, it tells me:

"eingeschaltet-- 12:00:00"

after running

(eingeschaltet means "turned on" or "set on" or the like)

Would Powershell be an option? You would save your sanity...

$Today = Get-Date
$Tomorrow = $Today.AddDays(1)

Write-Host "Tomorrow: $Tomorrow"

Yes, you can start a Powershell script from Batch:

Start Powershell and enter "Set-ExecutionPolicy Unrestricted"

(or sign your script, if other people need to run this) You can close Powershell now, we just needed it for this one-time step

In your Batch (assuming your script is called "Add-Days.ps1")

set script="Add-Days.ps1"
powershell.exe -NoProfile -File "%script%"
Dreami
  • 311
  • 1
  • 5
  • Hi, thanks for your prompt reply. When I run the script here is the output which I get ""2013-08-08 12:00:00". All, I need is to append +1 to the date only. I am not familiar with powershell but I wouldn't mind trying. The code which I posted is only part of the batch file operation, which uses this date as a input variable. Can powershell be executed within a batch file? – YoungDev Aug 08 '13 at 18:29
  • Sorry, see my edited answer, comments have so small space. However, I have no idea how to get the output back into Batch. – Dreami Aug 08 '13 at 18:41
0

I like Paul Tomasi's solution (proposed by GitaarLAB) but I found it has a small bug in that it doesn't work for "08" and "09" days of the month.

This is because the leading zero means the system thinks they are invalid octal numbers, and so with this statement:

set /a d=%date:~0,2%

It generates the following error with system dates such as 08/03/2017 or 09/03/2017 (assuming dd/mm/yyyy format on your dates):

Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).

I found the solution elsewhere on here (see dealing with octal variables in batch script), you have to add and then subtract 100 to deal with the leading zero, as per this statement:

set /a d=(1%d: =0%-100)

Here is the updated script in full:

::=====================================================
:: TOMORROW.BAT - Written by Paul Tomasi (c)2010
::
:: Function to return tomorrow's date
::    
:: updated to work for 08 and 09 days of the month
::             and for 08 and 09 months of the year
::=====================================================
@echo off

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

set /a d=(1%d: =0%-100)
set /a m=(1%m: =0%-100)

:loop

   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%

::------------------------------------------------
Community
  • 1
  • 1
SI_2000
  • 1
  • 1