Windows Batch File (.bat) to get current date in MMDDYYYY format.

Contributor Icon Contributed by seamonkey420 Date Icon July 24, 2005  
Tag Icon Tagged: Computer programming

This is a recipe I discovered while working on a few old skool dos batch files. This is a quick .bat file to create a folder based on the current date however, this sets it up in the MMDDYYYY format. Useful for those .bat scripters who need to use the current date variable in such a format.


Just copy and paste the text in quotes to a text file. Then rename file test.bat and run from command line to see (echo is on by default).

This can be used in a very powerful way, the FOR command can be reused and changed to query to see if a file exists and then if so create the folder, etc.

for now, lets start basic…

    echo on
    @REM Seamonkey’s quick date batch (MMDDYYYY format)
    @REM Setups %date variable
    @REM First parses month, day, and year into mm , dd, yyyy formats and then combines to be MMDDYYYY

    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=%mm%%dd%%yyyy%

this does nothing but setup the %date variable to be todays date in MMDDYYYY format so it can be called later in the script, etc..

peace
seamonkey420

Previous recipe | Next recipe |
 
  • Anonymous
    Here is a one liner
    <ul id="quote">For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set date=%%a-%%b-%%c)</ul>

    There may still be value in the first example it you need to access the seperate mm,dd,yyyy values but this works great for me.
  • Anonymous
    <ul id="quote">
    wattpuppy wrote:
    Here is a one liner
    </ul><ul id="quote">For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set date=%%a-%%b-%%c)</ul>

    There may still be value in the first example it you need to access the seperate mm,dd,yyyy values but this works great for me.

    hmm.. I tried your oneliner and only could get the day and the year.. I was missing the month.. I tried playing with the tokens and changing it to 1-4, but then I got Tue 08-16-2004 ..

    the first iteration on the initial page added an extra space which I didn't like.. thanks for the help!
  • apdreamland
    You should have tried 1-3 in Tokens......!!!!!!!! or try out this..........
    for /f "tokens=1,2,3 delims=/ " %%a in ('DATE /T') do set date=%%c-%%b-%%a

    I know I am too late to reply but it might help someone else in need........
  • Anonymous
    I actually fiddled with it and made it two lines.. but it's working..

    here it is..

    FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
    For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set date=%%a%%b%%c)
  • Willaim Shaffer
    I was able to use your format and add 2 more lines for the time to get a needed stamp
    FOR /F "TOKENS=1* DELIMS= " %%A IN ('TIME/T') DO SET CTIME=%%B
    For /f "tokens=1-4 delims=: " %%a in ('time /t') do (set time=%%a%%b%%c)
    Thanks for the help!
  • Anonymous
    Wierd! I currently use the exact code I sent posted to generate mm-dd-yyyy on a log file. What OS are you using? I'm running this on both Winxp and Win2000 Server.
  • Anonymous
    win 2003 server .. I can't see why there would be a difference...
  • Subhash
    Hi,

    Idea about getting the current date time in required format is good. I wanted to know how to subtract the required days from the current date.

    Like current date is 11/18/2005
    required date is 11/15/2005 i.e., subtract 3 days from current date.

    Then use that date in our script.
  • Anonymous
    Hi Subhash

    Getting calculation of date is no easy task.
    What O/S are you using.

    It's inportant because, "echo %Date%" or date /t would produce
    different results on different O/S and date formats ie.

    WinXP English Date 01/12/2005
    WinXP American Date 12/01/2005

    Win 2000 the same but adds the Weekday Tue 01/08/2005
  • mahen
    Hi,

    You can try the below. it works in XP.

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

    set /A dd=%dd%-3

    set Pdate=%mm%/%dd%/%yyyy%
    echo %Pdate%


    rgds
    Mahen
  • pbv kumar
    it works fantastic bu tmy problem is in format i want the date should be form 1 to 9 will be 01 ,02,03 & 09 like . kindly giv eme the solutions
  • hasanshaik
    can you please send the complete code which will execute in xp to cahnge the date format from m/d/yyyy to dd-mmm-yyyy respectively to my mail id " shaik.hassan.b@gmail.com"
  • davak
    <ul id="quote">
    Subhash wrote:
    Hi,

    Idea about getting the current date time in required format is good. I wanted to know how to subtract the required days from the current date.

    Like current date is 11/18/2005
    required date is 11/15/2005 i.e., subtract 3 days from current date.

    Then use that date in our script.</ul>

    Date Calculation In Batch Files XP/2000:
    http://www.tech-recipes.com/batch_file_programm...
  • Anonymous
    Seamonkey 420,

    I am trying to modify your code so it sets a variable for date in the format YYMMDD with no spaces. I tried to make the mods, but I just don't have the knowledge nor the time to figure this out.

    Can you assist?

    dklock
  • Anonymous
    Try this.


    for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
    for /f "tokens=1" %%u in ('time /t') do set t=%%u
    if "%t:~1,1%"==":" set t=0%t%
    rem set timestr=%d:~6,4%%d:~3,2%%d:~0,2%%t:~0,2%%t:~3,2%
    set datestr=%d:~6,4%%d:~0,2%%d:~3,2%
    set timestr=%t:~0,2%%t:~3,2%

    @echo %datestr%-%timestr%
    @echo %datestr%
    @echo %timestr%


    SparkByte
  • Anonymous
    i am working on a windows batch script that creates a zip file and adds the date to the file name, this works great for a full backup. I want to be able to set the initial date that the incremental backup begins, and then count up three days, and append the updated date to the zip file name.

    This is what i use to create the date:
    Set DD_MM_YYYY=%DATE:~4,2%_%DATE:~7,2%_%DATE:~10,4%

    then create file and add it to name as so:
    7a u -tzip e:backupincremental_%DD_MM_YYYY%.zip

    I would like to set the initial date for the file on lets say Monday 12_10_2007 then for Tues, Wed, and Thurs, append the file name to something like:
    incremental_%DD_MM_YYYY%_%Tues%.zip
    incremental_%DD_MM_YYYY%_%Wed%.zip
    incremental_%DD_MM_YYYY%_%Thurs%.zip

    Is it possible to set tues, wed, thurs as variables then script the rename of the file to reflect the new days?

    I guess I'm too new at this and could use some advice.

    Thanks,
  • TonyCH
    The 2nd line should have: echo %CDATE%
    instead of: DATE/T
  • hi
    this does not work
  • XJ Moonen
    Perhaps you should try this

    :: script for datestring in yyyymmdd format
    :: XJ Moonen Netherlands


    for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
    for /f "tokens=1" %%u in ('time /t') do set t=%%u

    if "%t:~1,1%"==":" set t=0%t%

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

    :: test if month < 10 if yes then add a leading zero
    if /i %m% LSS 10 set m=0%m%

    :: test if day < 10 if yes then add a leading zero
    if /i %d% LSS 10 set d=0%d%

    set ymd=%y%%m%%d%

    ::set the actual timestring ie 2034
    set timestr=%t:~0,2%%t:~3,2%

    :: set the filename
    set fname=Filename%ymd%_%timestr%

    :: show the filename
    echo %fname%
    c:\windows\system32\sleep.exe 10
  • Ryan
    why not just do this:

    set mydate=%date:~4,2%%date:~7,2%%date:~10,4%
  • Hersch
    Ryan, I love it, it is great one line no trickery with files etc. I expanded it further my requirements.

    echo Get the current date and time in YYYY-MM-DD-HH-MM-SS format
    SET isodt=%date:~10,4%-%date:~7,2%-%date:~4,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
    echo %isodt%

    Thanks a million
  • mike
    genius!, simple, elegant! thanks!
  • girija
    THANKS I GOT MY SOLUTION FROM THIS SITE, THANKS RYAN
  • Ketan Patel
    This is awesome and simple. Great stuff. Thanks a lot.
  • Rakesh Bhangre
    like my file in E drive - 06022009.DMP so how can copy this file in CD Drive using Batch file. my batch file is :-
    @echo off
    cls
    echo Copying started at %date% %time%
    xcopy "E:\%date%.DMP" "C:\Documents and Settings\Hp1\Local Settings\Application Data\Microsoft\CD Burning\BACKUP\*.*" /s /e /h /c /d /i /y
    xcopy "C:\Documents and Settings\Hp1\Local Settings\Application Data\Microsoft\CD Burning\BACKUP\*.*" "G:\BACKUP\*.*"
    PAUSE
    explorer g:
  • Marcus
    Perfect, exactly what i wanted, thanks for saving me 2 hours !
  • how to save file with filename+current date
    ex : myfile20090210
  • leif
    not the best at this yet dont even realy know what half what i posted mean but i just got it working with the Filename_"%PDATE%". if you dont want a gap inbetween file and date do Filename"%PDATE%". hope it helps.

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

    set /A dd=%dd%

    set Pdate=%yyyy%_%mm%_%dd%
    echo %Pdate%

    mkdir C:\MYFILE_"%PDATE%"\
  • Okey
    that helps
  • Some User
    For programmers in the USA, use THIS script instead, otherwise it won't work with the standard Windows US MM/DD/YYYY format.

    @Echo Off
    Set YYYY-MM-DD=%DATE:~6,4%-%DATE:~0,2%-%DATE:~3,2%
  • Many thanks. I started to write my own then quickly remembered writing bat files requires a fine mix of science and art with a dash of experence.
  • how can i combine the date and time to create a file name. please advise



    echo %date:~10,4%%date:~7,2%%date:~4,2%

    ren d:\temp\log.pdf Survey%date:~10,4%%date:~7,2%%date:~4,2%.pdf


    pause

    for /f "tokens=1-5 delims=:" %%d in ("%time%") do rename "hope.pdf" %%d-%%e.pdf
    pause
  • FP
    Or you can use %date% and parse it later in your script.
  • I had been using a method similar to yours but recently I found one that is even simpler:

    The %DATE% AND %TIME% variables contain the formated date and time so you an just extract the substrings using regular variable syntax

    set _DATETIME=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%

    In my case

    %DATE% contains "Tue 05/26/2009"
    so %DATE:~10,4% means to extract a substring 4 characters long starting from character 10

    If you weren't aware of that way of handling substrings for any variables, now you know.

    If you have a different default date format than mine you will have to adapt the parameters
  • Here's what I use. It queries the registry first to figure out what format the date is going to be returned in. It currently supports UK and US locales (as that's all I need to support at the moment). Trivial to extend it to support others.

    @echo off
    goto test

    :get_date_yyyymmdd
    setlocal enableextensions enabledelayedexpansion
    set _RV=
    set _ERR=0
    set _CMD=reg query "HKCU\Control Panel\International" /v sShortDate
    for /f "usebackq skip=2 tokens=3,* delims= " %%i in (`%_CMD%`) do (
    rem Amsterdam/Houston servers:
    rem Short date format (%%i): M/d/yyyy
    rem Sample %DATE%: Thu 05/29/2009
    rem London servers:
    rem Short date format (%%i): dd/MM/yyyy
    rem Sample %DATE%: 29/05/2009
    set D=!DATE!
    echo i: %%i
    echo D: !D!
    if "%%i"=="M/d/yyyy" (
    rem I'm assuming the day abbreviation will always be three chars
    rem (so we account for four in total, including the space). So
    rem far, I can attest that this is definitely the case on Wed &
    rem Thu ;-)
    set yyyy=!D:~-4!
    set mm=!D:~-10,-8!
    set dd=!D:~-7,-5!
    ) else if "%%i"=="dd/MM/yyyy" (
    set yyyy=!D:~-4!
    set mm=!D:~-7,-5!
    set dd=!D:~-10,-8!
    ) else (
    echo fatal: I don't understand this system's date format (%%i^)
    set _ERR=1
    )
    )
    set _RETVAL=!yyyy!!mm!!dd!
    endlocal & set _YYYYMMDD=%_RETVAL% & set _ERROR=%_ERR%
    exit /b %_ERROR%

    :test
    call :get_date_yyyymmdd
    echo formatted date: %_YYYYMMDD%
  • marcwentink
    Thanks Trent that is what I want: getting day month and year INDEPENDANT of the format used. I am going to study this.
  • dan
    Thanks guys... I really didn't have time to figure this out myself
    I was going to recreate xset32 for win64 in c++ or PERL, but this is much easier.
  • Ben Personick
    Erms, that IS pretty old school, try:

    Set TDate=%date:~10,4%%date:~4,2%%date:~7,2%


    1 line = better

    =D

    PS. Also it's bad form to use a variable with the same name as a command, so, don't do that. =P


    Peace,

    -B
  • marcwentink
    May this is usefull, especially since it is an example how to get all kinds of everything INDEPENDENT of the current language locale and date format settings using the Windows Scripting Host

    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ::
    :: This uses Windows Scripting Host to set variables
    :: to the current date/time/day/day_number
    :: for Win9x/ME/NT/W2K/XP etc
    :: Thanks go to Todd Vargo for his scripting
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    @echo off
    set TmpFile="%temp%.\tmp.vbs"
    echo> %TmpFile% n=Now
    echo>>%TmpFile% With WScript
    echo>>%TmpFile% .Echo "set year=" + CStr(Year(n))
    echo>>%TmpFile% .Echo "set yr=" + Right(Year(n),2)
    echo>>%TmpFile% .Echo "set month="+ Right(100+Month(n),2)
    echo>>%TmpFile% .Echo "set day=" + Right(100+Day(n),2)
    echo>>%TmpFile% .Echo "set hour=" + Right(100+Hour(n),2)
    echo>>%TmpFile% .Echo "set min=" + Right(100+Minute(n),2)
    echo>>%TmpFile% .Echo "set sec=" + Right(100+Second(n),2)
    echo>>%TmpFile% .Echo "set dow=" + WeekDayName(Weekday(n),1)
    echo>>%TmpFile% .Echo "set dow2=" + WeekDayName(Weekday(n))
    echo>>%TmpFile% .Echo "set iso=" + CStr(1 + Int(n-2) mod 7)
    echo>>%TmpFile% .Echo "set iso2=" + CStr(Weekday(n,2))
    echo>>%TmpFile% End With
    cscript //nologo "%temp%.\tmp.vbs" > "%temp%.\tmp.bat"
    call "%temp%.\tmp.bat"
    del "%temp%.\tmp.bat"
    del %TmpFile%
    set TmpFile=
    set stamp=%year%-%month%-%day%_%hour%.%min%.%sec%

    echo The year (YYyy) is "%year%"
    echo The year (yy) is "%yr%"
    echo The month is "%month%"
    echo The day (%dow%) is "%day%"
    echo The full weekday name is "%dow2%"
    echo.
    echo ISO 8601 Day-Of-Week number is "%iso%"
    echo.
    echo The hour is "%hour%"
    echo The minute is "%min%"
    echo The second is "%sec%"
    echo.

    echo The date and time stamp is "%stamp%"
    echo.
    echo time (hhmmss) (%hour%%min%%sec%)
    echo.
    echo date A (yyyymmdd) (%year%%month%%day%)
    echo date B (mmddyyyy) (%month%%day%%year%)
    echo date C (ddmmyyyy) (%day%%month%%year%)
    echo.
    echo date D [yymmdd] [%yr%%month%%day%]
    echo date E [mmddyy] [%month%%day%%yr%]
    echo date F [ddmmyy] [%day%%month%%yr%]
    :: datetime.bat
    ::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  • anon
    Brilliant Marc. Hopefully that will be useful with multiple users across Europe.
  • marcwentink
    Brilliantly searched on internet :) mostly
    I did not invent that all for myself.
    Marc Wentink
  • hasanshaik
    i just want to change the date format from m/d/yyyy to dd-mmm-yyyy and respectively please email me to shaik.hassan.b@gmail.com
  • ashmo
    I have read though all of these posts and still can't seem to figure out how to make a directory (folder) with the current date in the following format 09_08_13 (yy_mm_dd). Can anyone help me with this?
  • ahopici
    The solution using VB script is awesome.
    Eventually, extracting date is independent of regional setting on each particular computer.

    Thank you, marcwentink.
  • ASh
    how can I create a file.txt containing the name of a file with the date of yesterday?

    for example:

    I have to create a file named:
    script_ftp.txt -> within this file must be:

    get TCFILE720/TCCFASIE.F(dated yesterday) S: \ files \ TCCFASIE.F(dated yesterday)

    how can I create a file.txt containing the name of a file with the date of yesterday?

    for example:

    I have to create a file named:
    script_ftp.txt -> within this file must be:

    get TCFILE720/TCCFASIE.F20090825 S: \ files \ TCCFASIE.F20090825.dat
  • foAudits
    Thank you seamonkey420,

    This is just what I was looking for.

    ~Bill
  • David Thomas
    one liner (YYYYMMDD): %date:~10,4%%date:~4,2%%date:~7,2%
    output: 20090830
  • Thanks, this is exactly what I was looking for!
  • sajidkauser
    whats help i am trying to change the existing date in a txt file to the current system date....... can anyone help the date is located in line no 36 how to replace it
  • tsubasa_khun
    I trird your batch file then I added a few modifications to accomodate my backup goal. Nice work, excellent contribution!

    -Tsubasa
  • hainkurt
    here it is with one line

    for /f "tokens=1,2,3,4 delims=/ " %i in ('DATE /T') do set mydate=%j%k%l

    it will set mydate as

    Date /T
    Tue 01/12/2010

    set mydate=01122010
  • hasanshaik
    can you please send the complete code which will execute in xp to cahnge the date format from m/d/yyyy to dd-mmm-yyyy respectively to my mail id " shaik.hassan.b@gmail.com"
blog comments powered by Disqus