Windows Batch File (.bat): Get Current Date as MMDDYYYY

I discovered the following tech-recipe while working on a some old dos batch files. This tutorial describes a simple .bat file used to create a folder based on the date in the MMDDYYYY format.

This .bat file is useful for scripters who need to use the current date variable in the format MMDDYYYY.
First, copy and paste the text in quotes to a text file. Then rename the file test.bat, and run it from the command line to view it. (Echo is on by default.)

The steps in this tutorial can be used in a powerful way. The FOR command can be reused and changed to query to see if a file exists. If so, you can create the folder, etc.

For now, let’s start with the basics.

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

    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 set up the %date variable to be today’s date in MMDDYYYY format so that it can be called later in the script, etc.


About Jimmy Selix

Jimmy Selix is an early adopter that loves to be one of the first on the block to have the latest and greatest in technology and gadgets. Another love of his is being able to share his knowledge to others seeking it. Feel free to drop any comments or questions that you may have.
View more articles by Jimmy Selix

The Conversation

Follow the reactions below and share your own thoughts.

81 Responses to “Windows Batch File (.bat): Get Current Date as MMDDYYYY”

  1. October 03, 2008 at 5:44 am, pbv kumar said:

    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

  2. October 23, 2008 at 5:03 am, Anonymous said:

    The 2nd line should have: echo %CDATE%
    instead of: DATE/T

  3. December 11, 2008 at 5:35 am, hi said:

    this does not work

    • December 29, 2008 at 9:15 pm, XJ Moonen said:

      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

  4. January 27, 2009 at 1:06 am, Ryan said:

    why not just do this:

    set mydate=%date:~4,2%%date:~7,2%%date:~10,4%

    • February 07, 2009 at 12:37 am, Hersch said:

      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

      • June 25, 2012 at 11:20 pm, Jayson Franklin said:

        This should work:

        set scrptloc=%cd%
        cd %1
        set DATEFILE=dateFile.txt
        del %DATEFILE%
        date /t >> %DATEFILE%
        for /f “usebackq tokens=2-4 delims=^/ ” %%p in (%DATEFILE%) do set DIRNAME=%%r%%p%%q
        mkdir %DIRNAME%

    • March 19, 2009 at 12:15 pm, mike said:

      genius!, simple, elegant! thanks!

      • September 08, 2009 at 1:35 pm, Anonymous said:


    • January 20, 2010 at 6:40 am, Ketan Patel said:

      This is awesome and simple. Great stuff. Thanks a lot.

    • June 14, 2010 at 8:37 pm, Startingoftheweek said:

      thanks a lot Ryan..


    • February 02, 2011 at 5:35 pm, Bruce Lee said:

      This was a life-saver. Thank you.

    • June 19, 2012 at 12:36 pm, RL said:

      If the goal of finding the date is copying/moving files from “today”, the following is a simple way:

      FORFILES -p “C:\source” /d 0 -c “Cmd /C COPY @FILE C:\destination”

      Will copy files 0 days old. Can also limit to certain files (log files for instance) by adding -m *.log


  5. February 06, 2009 at 7:31 am, Rakesh Bhangre said:

    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
    echo Copying started at %date% %time%
    xcopy “E:%date%.DMP” “C:Documents and SettingsHp1Local SettingsApplication DataMicrosoftCD BurningBACKUP*.*” /s /e /h /c /d /i /y
    xcopy “C:Documents and SettingsHp1Local SettingsApplication DataMicrosoftCD BurningBACKUP*.*” “G:BACKUP*.*”
    explorer g:

  6. February 18, 2009 at 9:27 pm, Marcus said:

    Perfect, exactly what i wanted, thanks for saving me 2 hours !

  7. February 25, 2009 at 9:45 am, rvsdi said:

    how to save file with filename+current date
    ex : myfile20090210

    • February 27, 2009 at 2:26 pm, leif said:

      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%”

      • April 25, 2009 at 2:22 pm, Fuad said:

        that helps

  8. April 17, 2009 at 2:27 pm, Some User said:

    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%

    • June 18, 2010 at 4:55 am, Anonymous said:

      This was spot-on, I was using US timezone and no script was working. Finally, your script did the trick. Thanx a billion :)

  9. April 21, 2009 at 7:36 pm, Chris Felpel said:

    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.

  10. April 25, 2009 at 6:12 pm, James Lubuag said:

    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:templog.pdf Survey%date:~10,4%%date:~7,2%%date:~4,2%.pdf


    for /f “tokens=1-5 delims=:” %%d in (“%time%”) do rename “hope.pdf” %%d-%%e.pdf

  11. May 19, 2009 at 2:28 pm, FP said:

    Or you can use %date% and parse it later in your script.

  12. May 26, 2009 at 5:04 pm, Rarsa said:

    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

    • May 28, 2009 at 11:03 am, Trent Nelson said:

      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

      setlocal enableextensions enabledelayedexpansion
      set _RV=
      set _ERR=0
      set _CMD=reg query “HKCUControl PanelInternational” /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%

      call :get_date_yyyymmdd
      echo formatted date: %_YYYYMMDD%

      • August 06, 2009 at 9:29 am, Anonymous said:

        Thanks Trent that is what I want: getting day month and year INDEPENDANT of the format used. I am going to study this.

      • March 16, 2011 at 5:49 pm, AMan said:

        Perfect, exactly what I was looking for!

  13. June 04, 2009 at 2:06 pm, dan said:

    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.

  14. July 10, 2009 at 8:05 pm, Ben Personick said:

    Erms, that IS pretty old school, try:

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

    1 line = better


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



  15. August 07, 2009 at 8:48 am, Anonymous said:

    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 ISO 8601 Day-Of-Week number is “%iso%”
    echo The hour is “%hour%”
    echo The minute is “%min%”
    echo The second is “%sec%”

    echo The date and time stamp is “%stamp%”
    echo time (hhmmss) (%hour%%min%%sec%)
    echo date A (yyyymmdd) (%year%%month%%day%)
    echo date B (mmddyyyy) (%month%%day%%year%)
    echo date C (ddmmyyyy) (%day%%month%%year%)
    echo date D [yymmdd] [%yr%%month%%day%]
    echo date E [mmddyy] [%month%%day%%yr%]
    echo date F [ddmmyy] [%day%%month%%yr%]
    :: datetime.bat

    • December 01, 2009 at 12:28 pm, anon said:

      Brilliant Marc. Hopefully that will be useful with multiple users across Europe.

      • December 01, 2009 at 2:58 pm, Anonymous said:

        Brilliantly searched on internet :) mostly
        I did not invent that all for myself.
        Marc Wentink

    • January 15, 2010 at 4:08 pm, Anonymous said:

      i just want to change the date format from m/d/yyyy to dd-mmm-yyyy and respectively please email me to

    • July 27, 2010 at 12:48 pm, Sunilthomask said:

      Nice Thanks M8

    • February 09, 2011 at 10:03 am, Parthiv Raval said:

      This one is the best solution..thanks to marcwentink

  16. August 13, 2009 at 5:26 pm, Anonymous said:

    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?

  17. August 24, 2009 at 7:16 pm, Anonymous said:

    The solution using VB script is awesome.
    Eventually, extracting date is independent of regional setting on each particular computer.

    Thank you, marcwentink.

    • August 26, 2009 at 3:30 pm, ASh said:

      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

  18. August 28, 2009 at 2:41 pm, Anonymous said:

    Thank you seamonkey420,

    This is just what I was looking for.


  19. August 30, 2009 at 7:02 am, David Thomas said:

    one liner (YYYYMMDD): %date:~10,4%%date:~4,2%%date:~7,2%
    output: 20090830

  20. October 06, 2009 at 4:27 pm, Chris Hackett said:

    Thanks, this is exactly what I was looking for!

  21. November 24, 2009 at 5:58 pm, Willaim Shaffer said:

    I was able to use your format and add 2 more lines for the time to get a needed stamp
    For /f “tokens=1-4 delims=: ” %%a in (‘time /t’) do (set time=%%a%%b%%c)
    Thanks for the help!

  22. December 11, 2009 at 7:40 am, Anonymous said:

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

    • March 15, 2010 at 4:07 pm, Name said:


    • March 15, 2010 at 7:29 pm, Eric said:

      use “tokens=2,3,4″ to get the year instead of the day of week

  23. December 29, 2009 at 1:25 pm, Anonymous said:

    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

  24. December 30, 2009 at 1:31 am, Anonymous said:

    I trird your batch file then I added a few modifications to accomodate my backup goal. Nice work, excellent contribution!


  25. January 12, 2010 at 6:44 pm, Anonymous said:

    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

  26. January 15, 2010 at 4:01 pm, Anonymous said:

    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 ”

  27. January 15, 2010 at 4:05 pm, Anonymous said:

    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 ”

  28. February 10, 2010 at 8:20 pm, TooMuchCoffeeMan said:

    And now for something completely similar:

    For /F “Tokens=1-8 Delims=.:/ ” %%a In (‘Echo %Date% %Time%’) Do (Set TimeStamp=%%d-%%b-%%c %%e:%%f:%%g)

    My brain hurts!

  29. May 04, 2010 at 1:43 pm, Anonymous said:

    This works great!! I just need to make it do YYYMMDD

  30. May 14, 2010 at 9:01 am, vtdlbm said:

    this is for european time format (date /t =>

    for /f “tokens=1-3 delims=.” %i in (‘date/t’) do set dd=%i & set mm=%j & set yyyy=%k

  31. May 31, 2010 at 11:23 am, tim m said:

    I have a need to run this on different computers where the Windows short date format (depending on OS it’s set by Regional Settings applet in Control Panel) can vary depending on user’s (developers) tastes. Parsing is doable if, and only if, they are all using the same short date format which %date% also uses. I am afraid there is no solution to my situation except coding a batch file for each format – and if the user changes the format, they’re screwed. Even if a solution exists, it’s going to involve an ugly convoluted process of ascertaining the date format first and then parsing. Why did MS not also steal the %Y %M and %d env vars from unix when they ripped it off for DOS?!!!

    • June 07, 2010 at 11:47 am, ee said:

      my batch looks like this
      COPY “c:test.txt” “f:backup%date:~11,6%.%date:~7,2%.%date:~3,2% test.txt”

  32. June 04, 2010 at 7:43 am, pbv kumar said:

    can any one help me out how can i use month as JAN, FEB , and when month end how can i pick my month as previous one, i mean current month-previousmonth.for e.g 0n 01 Jun 10 i should get 31 May 10

  33. July 21, 2010 at 3:13 am, MeaCulpa said:

    Why not use the built-in string tailoring operators?
    for mmmmddyy:

    echo %DATE:~5,2%%DATE:~8,2%%DATE:~0,4%

    • August 20, 2010 at 2:19 pm, Patrick Ru said:

      echo %date:~4,10%

      echo %date:-=%

      echo %date:/=%

      echo %date:/=-%

    • August 20, 2010 at 2:28 pm, Patrick Ru said:

      set d=%date:-=%
      echo %d:~4,8%

  34. August 05, 2010 at 8:45 pm, Anonymous said:

    Thanks, that’s exactly what I was looking for! I was pissed at the DD-MM-YYYY that barged in every time I was creating a a folder based on the current date. I couldn’t find the windows exe files to solve this and I wasn’t used to seeing that, I think I never will. Great job, I managed to do what you did.

  35. August 09, 2010 at 6:38 am, Abc said:

    its not working..

  36. December 24, 2010 at 5:59 am, Imran Ikram said:

    well i have 1 question for all of you. i made a backup of my dump file with date and time. there is also command to then zip the same file in winzip format. winzip does not allow me to do that as when it starts zipping , the time has changed. forexample when my dmp file backup completes like imran 2010-24-10-12-20-12, and starts zip the time changes for a min or so. how to stop time when backup start so winzip completes its operation and backup the dmp file.
    command is

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

    exp file=e:db%isodt% .dmp
    cd c:program fileswinzip
    winzip32 -min -a -s e:db%isodt% .ZIP

    e:db%isodt% .dmp
    IF NOT EXIST e:db%isodt% .ZIP GOTO END
    thanks in advance

  37. January 17, 2011 at 8:49 am, Gargoba Honz said:

    jasonwhaley is absolutely right – instead of fiddling around here programmers should have killed mr. gates a long tie ago for this totally braindead implementation of such a basic thing like getting a date format working on every computer on the planet. extremely stupid!

  38. January 20, 2011 at 4:38 pm, Zulf said:

    The simple answer for converting from DD/MM/YYYY to a more script friendly version DD-MM-YYYY is:


    thats it, simple, use it to say save a log file, example:


  39. January 27, 2011 at 2:26 pm, Webstuff said:

    echo %date:~-4,4%%date:~-7,2%%date:~0,2%

    Does the job in YYYYMMDD format in one line

    • February 09, 2011 at 12:57 am, Fixit said:

      The DAY doesn’t show up. Just the Year, Month, and day of week.

      • March 15, 2011 at 7:21 pm, Dv400b said:

        Sounds like your date format is in “Day Month/Date/Year” format, in which case you want


        • March 22, 2011 at 11:23 pm, Stephen Jung said:

          Can you also get the quarter?

  40. February 01, 2011 at 5:34 pm, Atsuine said:

    need some help, i need a DOS script that will return previous date but only weekdays. i.e say Monday is 2011-01-31 and the script will return 2011-01-28, all help appreciated.


  41. March 08, 2011 at 10:11 am, Mihai Mandis said:

    There’re diferent data format given by “echo %date%_%time%” command.
    1. 03/08/2011_12:05:31.12
    2. Thu 03/03/2011_07:38 PM
    3. 2011-03-08_12:05:31.12
    It might be quite difficult to get the date and time by parsing a string given by “echo %date%_%time%” command.
    I’ve choosen to use “” executable to get data and time:

  42. March 14, 2011 at 9:41 am, Frankyke said:

    Hi all, how to take out the “0″ in month?

    The date is 11/02/2011. If it is 02, I may just want single digit “2″ instead of “02″. If it is 12 then may keep both digits “12″.

    thanks all.

  43. March 16, 2011 at 3:16 pm, Kouliscon said:

    Hello all,
    i am usign with GPO a batch file at logon and logoff of the computers which copies a specific location to a network location for backup reasons.
    The data has become big and i need to add to the batch file ONLY the files which were CREATED the last 90 days.
    anybody can help?
    the batch file command is:
    xcopy *.* /s /r /e /d /h /y /exclude:\ \
    thx in advance

  44. April 04, 2011 at 12:19 pm, Venkat Lvr said:

    can you please send the complete code which will execute in xp to show date format from dd-mm-yyy to dd-mmm-yyyy respectively to my mail id ”
    Eg: 01-04-2011 to 01-Apr-2011.

  45. June 24, 2011 at 7:10 pm, Idris_tas said:


  46. December 07, 2011 at 5:34 am, jymurthy said:

    How can I print previous day’s date

  47. December 16, 2011 at 2:57 am, minipop4747 said:

    I want to get 2 days back days in YYYYMMDDHHMM format to create a batch file(need cmd command).

    as an example today is : 201112161200 and i want to get 2 days back date as below 201112141200

    ECHO %date:~10,4%%date:~7,2%%date:~4,2%%time:~0,2%%time:~3,2%

    Mention command gives the current date in above format 201112161200.

    But i need to get 2 days back date from the mention format

    if any one knows how to do this please help me

  48. April 11, 2012 at 10:07 am, sergeiv said:

    It can be done much more simpler:
    FOR /F “TOKENS=1* DELIMS= ” %%A IN (‘DATE /T’) DO SET mydate=%%A
    FOR /F “TOKENS=1,2,3 DELIMS=.” %%A IN (‘echo %mydate%’) DO SET mydate=%%C%%B%%A
    echo sergeiv_%mydate%.bkf

    The first line removes space after the date, the second one formats the date, the third one prints the result as a filename example.

  49. May 07, 2012 at 2:34 am, Script Guy said:

    How about this for something simple with just one or two lines to fit the needs regardless???

    for /f “tokens=2-4 delims=/ ” %%A in (“%date%”) do set “mm=%%A” do (& set “dd=%%B”) do (& set “yyyy=%%C”)

    set yy=%DATE:~-2%

    You could also just put your variable in whatever format you need to the dd/yyyy/mm format to be; example:

    for /f “tokens=2-4 delims=/ ” %%A in (“%date%”) do set “dt=%%C%%A%%B”

    You can use this for a timestamp in the 12 hour intervals and include either AM or PM too:

    for /f “tokens=1-3 delims=: ” %%A in (‘Time /T’) do set “HRTime=%%A%%B%%C”


  50. May 31, 2012 at 9:00 pm, RAJUKR said:

    Simply superb and Thanks a lot for your tips

  51. June 15, 2012 at 12:11 pm, thierry said:

    Even simplier:
    in VBS file “buildSetDateTimeVars.vbs” :

    WScript.Echo “set Date_yyyy=” + CStr(Year(n))
    WScript.Echo “set Date_yy=” + Right(Year(n),2)
    WScript.Echo “set Date_mm=”+ Right(100+Month(n),2)
    WScript.Echo “set Date_dd=” + Right(100+Day(n),2)
    WScript.Echo “set Time_hh=” + Right(100+Hour(n),2)
    WScript.Echo “set Time_mm=” + Right(100+Minute(n),2)
    WScript.Echo “set Time_sec=” + Right(100+Second(n),2)
    WScript.Echo “set Date_dow=” + WeekDayName(Weekday(n),1)
    WScript.Echo “set Date_dowlong=” + WeekDayName(Weekday(n))

    in batch file (assuming they’re on same path), we loop on script result and execute command :
    for /F “TOKENS=1 eol=/ DELIMS=/” %%F in (‘cscript %~dp0buildgetDateBatch.vbs’) do %%F

    The %~dp0 stands for “disk” “path” of “batch itself” : we ensure that the script will be searched in same path a calling batch.

    No need to create /execute/clean temp files.

  52. June 25, 2012 at 5:22 pm, Robert Ridenour said:

    Here’s an easy way of handling the date/time format, making it easy to put in any format you want…

    REM Get Date/Time numbers
    Set Year=%date:~10,4%
    Set Month=%date:~7,2%
    Set Day=%date:~4,2%
    Set Hour=%time:~0,2%
    Set Minute=%time:~3,2%
    Set Second=%time:~6,2%

    (Note: I’m in the USA, so if you aren’t, you may need to adjust the offsets above – this was covered I think in a prior post)

    Now you can format a file along these lines (or easily change it to however you want):