Parsing Date in a Batch File

Today I was trying to generate a filename based on the current date in a batch file. After doing some searching here is what I came up with:

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @( 
	Set Month=%%A
	Set Day=%%B
	Set Year=%%C

@echo DAY = %Day%
@echo Month = %Month%
@echo Year = %Year%

This allows for me to format the date anyway I want. I didn't know that the /F command of the For loop could tokenize line output. Thats pretty cool, although there should be a little better string support for batch files.

Also posted on my new blog.


  • I hope none of your users ever set their PC to a non-US locale.

  • Yes that is true but this is just a simple example which can be generalized to other strings.

  • Yes Wes, really, what were you thinking?


  • Thanks Bernd!

    See how easy that was to translate to another language. A simple example which can be generalized was all I was after ;)

  • I had to change it for my 2003 server with MMddYYYY

    echo on

    @For /F "tokens=1,2,3 delims=/ " %%A in ('Date /t') do @(
    Set Month=%%A
    Set Day=%%B
    Set Year=%%C
    Set fDate=%%C-%%A-%%B
    Set fileDate="F:\BestBackup\"

    @echo %fDate%
    @echo %fileDate%

  • Does anyone know if its possible to get the day in string format such as 'FRIDAY'? i want to use it in a if-statement so I can run a file on a specific date.

  • If you 'date /t' gives you output like 'Tue 09/26/2006' then below can provide you three letters of a day like 'Tue" for Tuesday.

    @For /F "tokens=1 delims= " %%A in ('Date /t') do @(
    Set DayName=%%A

    @echo DAY = %DayName%

  • Thanks Wez, just what I was looking for.

  • Thanks a lot, that was exactly what I was looking for, too. the /F in FOR statement should be mentionned in every batch tuto :))

    Thanks again, Wes !

  • Very Useful One ,


  • Csn we get the time of the folders when there are created. I am trying to parse folder structure according to the latest timestamp. i want to get the time of latest created folder

  • helped me a lot!
    thanks sooo much

  • Thanks for posting.

  • Why not just do this?

    Variables gives today's date in a format like 2007_09_17 -> YYYY_MM_DD

    Take the variable %date%, then return all characters from 4 onwards. Counting starts at 0.

    %date:~10,4% means give me the first four characters after the 10th one in the string.

    set FileDate=%date:~10,4%_%date:~4,2%_%date:~7,2%


  • how about if "date /t" returns 10Oct07?

  • Hi,
    This is the great blog site to learn.I did tried to use the same command but it keeps prompting me %%A was unexpected at this time.Can you kindly help in progressing

  • set folder=%date:~-4,4%%date:~-10,2%%date:~-7,2%archive

    the :~-0,4 is actually a substring command

  • Easiest way is to just use PowerShell :)

    I know that's a worthless comment, but it's really amazing to look back at the convoluted way of batch parsing as compared to what's available in PowerShell. Going back to CMD is almost as bad as when I had to switch from Bash to CMD.

  • Agreed powershell is the way to go... However it just doesn't have the mass distribution yet.

  • Mine refuse to give me the year,

    Using on XP

    Will this work on server 2003 x64

  • Thanks for the post, worked perfectly fine for me, though i changed the tokens to 1-1 which gave me the day of the week, which was what i was looking for, as i was writing a backup script based on the day of the week.

  • Why not change your regional settings so that the short date format is YYYY-MM-DD, or whatever you want it to be? Then you can just use %date%

  • SET Year=%DATE:~6,4%
    SET Month=%DATE:~3,2%
    SET Day=%DATE:~0,2%
    Of course this depends on regional date setting on your pc this is using format dd,mm,yyyy

  • Thanks for the date format in YMD it helped me a lot to take daily backup.

  • Excellent blog. Is there any way to get the Month in MMM format (Sep,Dec etc.)

  • Thanks - this post is really useful. There may well be a better way, but the following will convert the month to a Mid name:

    @echo off
    SET Month=%DATE:~3,2%
    if %Month%==01 set Month=JAN
    if %Month%==02 set Month=FEB
    if %Month%==03 set Month=MAR
    if %Month%==04 set Month=APR
    if %Month%==05 set Month=MAY
    if %Month%==06 set Month=JUN
    if %Month%==07 set Month=JUL
    if %Month%==08 set Month=AUG
    if %Month%==09 set Month=SEP
    if %Month%==10 set Month=OCT
    if %Month%==11 set Month=NOV
    if %Month%==12 set Month=DEC
    echo %Month%

  • Fantastic. this is exactly what i needed to create a series of backups.
    Thanx a lot.

  • I am using Windows XP Pro... whoever gave the last solution deserves a big thanks.. I was looking for this code... but you made a little mistake.. coz Win XP uses this format Day mm-dd-yyyy the actual coding should be :

    @echo off

    SET Month=%DATE:~4,2%

    if %Month%==01 set Month=JAN

    if %Month%==02 set Month=FEB

    if %Month%==03 set Month=MAR

    if %Month%==04 set Month=APR

    if %Month%==05 set Month=MAY

    if %Month%==06 set Month=JUN

    if %Month%==07 set Month=JUL

    if %Month%==08 set Month=AUG

    if %Month%==09 set Month=SEP

    if %Month%==10 set Month=OCT

    if %Month%==11 set Month=NOV

    if %Month%==12 set Month=DEC

    echo %Month%

  • Very very useful :) Thanks a lot.

  • Hi, I need to change to date format of a file xxxxx.txt2009-05-10


    Is there an easy wasy to do this?

  • Files in windows exp can't contain the "/" character, it causes issues.

  • For /f "tokens=1,2,3,4,5 delims=/. " %%a in ('date/T') do set CDate=%%d-%%b-%%c

    @echo data = %CDate%

    This is in the Year-Month-Day that is most common now days

  • This is what I use. Works for all our servers and workstations.

    FOR /F "tokens=1-6 delims=.-/: " %%A IN ("%DATE% %TIME%") DO (
    FOR /F "tokens=2-4 skip=1 delims=(.-/)" %%G IN ('VER^|DATE') DO (
    SET %%G=%%A
    SET %%H=%%B
    SET %%I=%%C
    SET HH=%%D
    SET II=%%E
    SET SS=%%F
    ECHO YY = %YY%
    ECHO MM = %MM%
    ECHO DD = %DD%
    ECHO HH = %HH%
    ECHO II = %II%
    ECHO SS = %SS%

    One issue is DST. There seems to be no way to deal with this from the command prompt.

  • Though one issue exists if a user is using long date parts for their short date (set via the regional settings in the control panel).

    As long as the parts are dd, mm and yyyy and separated by one of '-./' then all should be well. The order is all handled - yyyy-mm-dd, mm-dd-yyyy, dd-mm-yyyy, etc.

  • And a Swedish verison:
    @For /F "tokens=3,2,1 delims=- " %%A in ('Date /t') do @(
    Set Day=%%A
    Set Month=%%B
    Set Year=%%C

  • Another option if you have to deal with different date formats on different machines:

    FOR /F "usebackq skip=4 tokens=3" %%i in (`reg.exe query "HKCU\Control Panel\International" /v sShortDate`) DO SET DATE_FORMAT=%%i
    IF %DATE_FORMAT% EQU dd/MM/yyyy SET FORMATTED_DATE=%date:~-4,4%-%date:~-7,2%-%date:~0,2%
    IF %DATE_FORMAT% EQU M/d/yyyy SET FORMATTED_DATE=%date:~-4,4%-%date:~4,2%-%date:~-7,2%
    IF "%DATE_FORMAT%" EQU "" SET FORMATTED_DATE=UnknownDateFormat%date%

  • can any one help me out how to pass the month in the foramat of JAN, FEB, Mar like and when the last day of the month is shuld pick the last month instead of current month .

  • My system time is in IST i.e. +05:30hrs from UTC. For 1800UTC data of the date, by the tme data is available, the system date has changed to next date. During the month, the system date minus one day is executed by the batch file. On the last day of the month, both date and month needs to be of the previous day and month. I am able to make the batch file to make the date as previous but unable to make the month as previous month.
    Example: Data of 30Apr is being downloaded by 0940 PM UTC. The system date is 01May and time is 0310 AM IST. I am able to get date as 30 but not the month as Apr.

    Pls help

  • Thanks everyone, you helped me to create my first batch file!

Comments have been disabled for this content.