Previous / yesterday’s date in DOS batch file

Here is the script to get previous date output in DOS using bat files. This is very useful when we take backup of files with previous date name. (for example backup_daily_20072010.zip ) or can set bat files to get last 7 days backup to a zip.  According to your requirement you can get any previous date from this script.  See the variable values. Following Script just show today’s and yesterdays date. Most of the script having some problems when it run on 1st if any moth. but here it is solved. you can customize the date formats also. using variables %DD%, %MM%, %YYYY%  Please comment to this post If you have any query .

@echo off

for /f "tokens=1" %%i in ('date /t') do set thedate=%%i

set mm=%thedate:~3,2%
set dd=%thedate:~0,2%
set yyyy=%thedate:~6,4%
echo.
echo.
echo Today : %dd%.%mm%.%yyyy%
echo.

if %dd%==08 (
set dd=8 ) else (
if %dd%==09 (
set dd=9 ) )

if %mm%==08 (
set mm=8 ) else (
if %mm%==09 (
set mm=9 ) )

set /A dd=%dd% - 1
set /A mm=%mm% + 0

if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31

goto ERROR

:SET31
set /A dd=31 + %dd%
goto DONE

:SET30
set /A dd=30 + %dd%
goto DONE

:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto DONE

:SET29
set /A dd=29 + %dd%

:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
set YESTERDAY=%dd%.%mm%.%yyyy%
echo.
echo YESTERDAY : %YESTERDAY%
echo.
echo.
pause

About Albin Sebastian

I am a Technology Blogger, System Administrator by profession and webmaster by passion. Technology blogger, Active in Online and offline tech communities.

Check Also

Batch Script to check DNS

Batch Script to check DNS results from different DNS Servers

Normal DNS propogation maximum time is 24 hr to 48 hrs. but usually servers start …

Comments

  1. How do I use this script, I have tried to put it into a dos .cmd file but the dos screen just comes up and disappears rather than stay open at the pause prompt as I thought it would.  I need to use this script to copy some files of certain dates to different folders etc.

  2. Ah that seems OK but just getting an error on YESTERDAY for some reason, TODAY is OK but YESTERDAY puts an extra 0 in the month, see output below.
     

    Today : 02.09.2010
     
    Invalid number.  Numeric constants are either decimal (17),
    hexadecimal (0x11), or octal (021).
    YESTERDAY : 01.009.2010
    Press any key to continue . . .
     
    Thanks in advance for your help.

  3. There is a bug in the code.
    It will not work properly If the month is August or September. The bug is fixed with following code. place this code before  line ” set /A dd=%dd% – 1 ”
    Code :

    if %mm%==08 (
    set mm=8 ) else (
    if %mm%==09 (
    set mm=9 ) )

  4. Arun
     
    The code worked ok for a week but has now reverted to the problem with the day coming as 008, see below.
     

    Today : 08.09.10
    Invalid number.  Numeric constants are either decimal (17),
    hexadecimal (0x11), or octal (021).
    YESTERDAY : 008.09.10
    Press any key to continue . . .
    mt today 0080910
    Press any key to continue . . .
     
    Here is the code I have in my script, any ideas why this has done this again?

    @echo off
     
    for /f “tokens=1” %%i in (‘date /t’) do set thedate=%%i
     
    set mm=%thedate:~3,2%
    set dd=%thedate:~0,2%
    set yy=%thedate:~8,2%
    echo.
    echo.
    echo Today : %dd%.%mm%.%yy%
    echo.
    if %mm%==08 (
    set mm=8 ) else (
    if %mm%==09 (
    set mm=9 ) )
     
    set /A dd=%dd% – 1
    set /A mm=%mm% + 0
     
    if /I %dd% GTR 0 goto DONE
    set /A mm=%mm% – 1
    if /I %mm% GTR 0 goto ADJUSTDAY
    set /A mm=12
    set /A yy=%yy% – 1
     
    :ADJUSTDAY
    if %mm%==1 goto SET31
    if %mm%==2 goto LEAPCHK
    if %mm%==3 goto SET31
    if %mm%==4 goto SET30
    if %mm%==5 goto SET31
    if %mm%==6 goto SET30
    if %mm%==7 goto SET31
    if %mm%==8 goto SET31
    if %mm%==9 goto SET30
    if %mm%==10 goto SET31
    if %mm%==11 goto SET30
    if %mm%==12 goto SET31
     
    goto ERROR
     
    :SET31
    set /A dd=31 + %dd%
    goto DONE
     
    :SET30
    set /A dd=30 + %dd%
    goto DONE
     
    :LEAPCHK
    set /A tt=%yy% %% 4
    if not %tt%==0 goto SET28
    set /A tt=%yy% %% 100
    if not %tt%==0 goto SET29
    set /A tt=%yy% %% 400
    if %tt%==0 goto SET29
     
    :SET28
    set /A dd=28 + %dd%
    goto DONE
     
    :SET29
    set /A dd=29 + %dd%
     
    :DONE
    if /i %dd% LSS 10 set dd=0%dd%
    if /I %mm% LSS 10 set mm=0%mm%
    set YESTERDAY=%dd%.%mm%.%yy%
    echo.
    echo YESTERDAY : %YESTERDAY%
    echo.
    echo.
     
    PAUSE
    ECHO mt today %DD%%MM%%YY%
    pause
     

  5. @Michael

    Yes we diagnosed the problem, it was a problem with values 8 & 9 . this problem repeats today ( 08-09-2010). we already set an option for month 08 & 09. we will do the same thing here also.

    Thanks for notifying us the errors

    Code is updated

  6. I just added the following after the fix for the month issue,  seems  to be OK
     

    if %dd%==08 (
    set dd=8 ) else (
    if %dd%==09 (
    set dd=9 ) )

  7. Great! Simply Great! Thank you for this script. Now it is easy to make backups of logs in WINDOWS environment.

  8. The above batch file is not working and giving below output
    Today : 16.Ju.-11
    YESTERDAY : 15.00.-11

    I want the output like

    15-06-2011

  9. As per the today output I assume that your computers date format is as below

    “dd.MMM.yy”

    The script will give output if  the date format is “dd.MM.yyyy”

    Change your date format to above format and try,

     

  10. Thanks a lot Albin, its working fine now,
    I have one more question can I change the date format of my machine for English United States M/d/yyyy

    Please let me know and update or send the script to my mail id
    [email protected]

  11. Hi

    Any one can help us

    XCOPY D:\*.*  E:\”%date:~7,2%-%date:~4,2%-%date:~10,4%” /D:7-2-2011 /s /c /i /y

    here i need to enter /D:7-2-2011 manually every day

    any one help us by modifing auto matically taking 2 days back date

  12. Source:D:\*.* 
    Dest   :E:\”%date:~7,2%-%date:~4,2%-%date:~10,4%”(It creats folder with SYS date)

     /D /s /c /i /y(/D) Date modified if i menction date(M-D-YY) it will copy only modifies files dated on & after the date.this date i need to type every 2 days .

    exp
    if i run the batch file tday(7-11-2011), i need to setup last 2 days date:/D(7-9-2011)

    like this every 2days i need to change the date

    thanks
    nagi

  13. Here we are using date format “DD.MM.YYYY”

    You can modify following line as per your format  

    "set YESTERDAY=%dd%.%mm%.%yyyy%"

     

    For two day backs result change following line

    set /A dd=%dd% - 1
    
    to 
    
    set /A dd=%dd% - 2

     

     

  14. Hi albin i have a problem with brackets in the if parts

    <code>
    if %dd%==08 (
    set dd=8 ) else (
    if %dd%==09 (
    set dd=9 ) )

    if %mm%==08 (
    set mm=8 ) else (
    if %mm%==09 (
    set mm=9 ) )
    </code>
    there is a error like this
    Today: ..

    Not expected ( at this time.
    which can be the problem.
     
    Thanks in advance