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

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

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 7, 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 2, 2007 9:11 AM by Allyn

# re: Parsing Date in a Batch File

helped me a lot!

thanks sooo much

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

# re: Parsing Date in a Batch File

Thanks for posting.

Sunday, September 9, 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

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 1, 2008 12:06 PM by Rick

# 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 1, 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 8, 2009 11:08 AM by toufiqkm

# re: Parsing Date in a Batch File

Very very useful :) Thanks a lot.

Tuesday, June 9, 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

# re: Parsing Date in a Batch File

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

@ECHO OFF

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.

Tuesday, December 1, 2009 5:00 AM by Richard Quadling

# re: Parsing Date in a Batch File

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.

Tuesday, December 1, 2009 5:06 AM by Richard Quadling

# re: Parsing Date in a Batch File

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

)

Tuesday, January 5, 2010 4:09 AM by Mejner

# re: Parsing Date in a Batch File

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 yyyy-MM-dd SET FORMATTED_DATE=%date%

IF "%DATE_FORMAT%" EQU "" SET FORMATTED_DATE=UnknownDateFormat%date%

echo %FORMATTED_DATE%

Wednesday, May 26, 2010 3:09 PM by Luis Marques

# re: Parsing Date in a Batch File

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 .

Friday, June 4, 2010 6:51 AM by pbv kumar

# re: Parsing Date in a Batch File

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

Wednesday, August 4, 2010 2:57 AM by PBV Kumar

# re: Parsing Date in a Batch File

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

Monday, February 14, 2011 7:26 PM by Roger