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.

Published Thursday, August 18, 2005 7:13 PM by puzzlehacker

Comments

# re: Parsing Date in a Batch File

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

Friday, August 19, 2005 5:48 AM by Joe

# re: Parsing Date in a Batch File

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

Friday, August 19, 2005 10:17 AM by Wes Haggard

# re: Parsing Date in a Batch File

Yes Wes, really, what were you thinking?
ALWAYS LOCALIZE YOUR STRINGS!!!!! DUH :-P

Saturday, August 20, 2005 5:24 PM by InesVT

# re: Parsing Date in a Batch File

Here is a German Version
:: C:\>date /t
:: 26.08.2005

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

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

Friday, August 26, 2005 4:50 AM by Bernd

# re: Parsing Date in a Batch File

Thanks Bernd!

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

Friday, August 26, 2005 10:28 AM by Wes Haggard

# re: Parsing Date in a Batch File

hehehe :)

Saturday, August 27, 2005 12:11 AM by InesVT

# re: Parsing Date in a Batch File

Just the ticket, Thank you very much

Wednesday, July 05, 2006 6:07 AM by dbclark

# re: Parsing Date in a Batch File

I had to change it for my 2003 server with MMddYYYY cls 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\Best_%Year%%Month%%Day%.zip" ) @echo %fDate% @echo %fileDate%

Saturday, July 22, 2006 1:14 AM by Em

# re: Parsing Date in a Batch File

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.

Thursday, September 07, 2006 11:36 AM by Mike

# re: Parsing Date in a Batch File

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%

Tuesday, September 26, 2006 7:10 AM by Ali

# re: Parsing Date in a Batch File

Thanks Wez, just what I was looking for.

Sunday, November 12, 2006 8:01 AM by Nigel Peck

# re: Parsing Date in a Batch File

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 !

Monday, November 27, 2006 5:39 AM by Simon F.

# re: Parsing Date in a Batch File

Very Useful One ,

thanks

Sunday, July 29, 2007 2:18 PM by TNLS Srinivas

# re: Parsing Date in a Batch File

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

Thursday, August 02, 2007 9:11 AM by Allyn

# re: Parsing Date in a Batch File

helped me a lot!

thanks sooo much

Friday, September 07, 2007 9:20 PM by anonymous

# re: Parsing Date in a Batch File

Thanks for posting.

Sunday, September 09, 2007 9:56 AM by Aneel

# re: Parsing Date in a Batch File

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%

FileName_%FileDate%.txt

Friday, October 12, 2007 11:36 PM by Bob

# re: Parsing Date in a Batch File

Seems Microsoft has made it simpler: support.microsoft.com/.../555314

Thursday, October 18, 2007 1:47 AM by JaM

# re: Parsing Date in a Batch File

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

Tuesday, October 30, 2007 2:13 AM by worldwide

# re: Parsing Date in a Batch File

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

Thursday, March 27, 2008 5:28 AM by Deepika

# re: Parsing Date in a Batch File

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

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

Tuesday, April 01, 2008 12:06 PM by Rick

# re: Parsing Date in a Batch File

Friday, April 25, 2008 2:36 PM by Airton

# re: Parsing Date in a Batch File

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.  

Tuesday, April 29, 2008 12:07 AM by JaredPar

# re: Parsing Date in a Batch File

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

Tuesday, April 29, 2008 12:22 AM by puzzlehacker

# re: Parsing Date in a Batch File

Mine refuse to give me the year,

Using on XP

Will this work on server 2003 x64

Monday, May 19, 2008 6:16 AM by Hendrik Beukes

# re: Parsing Date in a Batch File

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.

Wednesday, June 11, 2008 9:20 AM by Anand

# re: Parsing Date in a Batch File

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%

Wednesday, July 30, 2008 7:35 AM by John

# re: Parsing Date in a Batch File

thanks a lot mate.. just exactly the code that I need :D

Friday, August 01, 2008 1:21 AM by bartho

# re: Parsing Date in a Batch File

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

Wednesday, October 15, 2008 7:47 PM by wayne

# re: Parsing Date in a Batch File

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

Thursday, October 23, 2008 11:06 AM by sshivaprakash@hotmail.com

# re: Parsing Date in a Batch File

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

Benjamin_massa@hotmail.com

Sunday, November 16, 2008 9:38 AM by ben

# re: Parsing Date in a Batch File

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%

Wednesday, February 18, 2009 7:38 AM by Joel

# re: Parsing Date in a Batch File

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

Thanx a lot.

Tuesday, April 14, 2009 3:55 AM by iceblitz

# re: Parsing Date in a Batch File

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%

Friday, May 08, 2009 11:08 AM by toufiqkm

# re: Parsing Date in a Batch File

Very very useful :) Thanks a lot.

Tuesday, June 09, 2009 8:11 AM by Chandra

# re: Parsing Date in a Batch File

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

to

xxxxx.txt2009/05/10

Is there an easy wasy to do this?

Wednesday, June 10, 2009 12:22 AM by cbryant

# re: Parsing Date in a Batch File

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

Friday, June 12, 2009 3:02 PM by plowther

# re: Parsing Date in a Batch File

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

Wednesday, October 14, 2009 7:22 PM by Buffme

Leave a Comment

(required) 
(required) 
(optional)
(required)