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, I Blog about technology related articles, Active in online and offline tech communities

25 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. Hi Michael,

    That problem happens due to copy paste that code from this page. The formatting for code is gone, Now that is fixed so you can copy paste the code

  3. 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.

  4. 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 ) )

  5. Fantastic thanks very much, very strange bug isn’t it?

  6. @ Arun,

    Thanks for your support,

    The bug fixed in above code.

  7. 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
     

  8. @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

  9. 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 ) )

  10. @Michael

    yes same thing.

    I updated in the above code.

  11. good discussion going on here… thanks to all of you  and ALBIN for the interesting post…:0

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

  13. thanks, this is extremely useful

  14. 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

  15. 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,

     

  16. 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
    contactahmad@rediffmail.com

  17. 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

  18. its very urgent

  19. Can you explain your requirement ? where you want to get the output ?

  20. Hi Albin
    /D:7-2-2011 this area needs to take date 2 days back from sys date

    thanks
    nagi

  21. 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

  22. 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

     

     

  23. If passible can you please send the txt file to my mail id

    nags.basis@gmail.com

  24. I feel the line of “goto ERROR” useless, isn’t it?

  25. 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

Leave a Reply

More in Programming (5 of 10 articles)