Batch File Date

Hello,

I am trying to get the currnet date so that when I create a file it has the date in it, however this has not been easy !..

I am trying to use this to create the date (see code below). However this this runs it seems to create the Pdate as _03_2009 which is strange as I was expecting 2009_03_30 ?

Can someone tell me what is wrong with the below. I am using this batch files on Windows XP Compuiters only.
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%

Open in new window

gisvpnAsked:
Who is Participating?
 
sirbountyConnect With a Mentor Commented:
Try this:

set yyyy=%date:~-4%
set mm=%date:~4,2%
set dd=%date:~7,2%
set Pdate=%yyyy%_%mm%_%dd%

Open in new window

0
 
gisvpnAuthor Commented:
Hello,

Thanks for the post, I think its there however I get this now :

2009_3/_00

I am trying to get 2009_03_30

;)
0
 
sirbountyCommented:
Some display the day as well (mine does) - apparently yours does not...try this:
set yyyy=%date:~-4%
set mm=%date:~0,2%
set dd=%date:~3,2%
set Pdate=%yyyy%_%mm%_%dd%

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Lukasz ChmielewskiCommented:
try this
set yyyy=%date:~-4%
set mm=%date:~5,2%
set dd=%date:~8,2%
set Pdate=%yyyy%_%mm%_%dd%

Open in new window

0
 
t0t0Commented:
Please try the following:

set Pdate=%date:~-4%_%date:~3,2%_%date:~0,2%
0
 
gisvpnAuthor Commented:
Excellent thank you.
0
 
gisvpnAuthor Commented:
I just noticed that the month and day appear to be swapped around as in DD = 03 (when it should be 30)..

Can I just change it to as below :

Could I also ask what the end bits mean (i.e. ~5,2%) ?

Thanks,

GISVPN
 

set yyyy=%date:~-4%
set dd=%date:~5,2%
set mm=%date:~8,2%
set Pdate=%yyyy%_%mm%_%dd%

Open in new window

0
 
gisvpnAuthor Commented:
what are the difference between all the posts ?
0
 
sirbountyCommented:
Glad I could help - thanx for the grade! :^)
0
 
Lukasz ChmielewskiCommented:
I'm glad also that my correct answer was not graded :)
this is because the date is in the format: 2009-03-30
counting index from 0 is also applied to "-" sign
0 1 2 3 4 5 6 7 8 9
----------------------
2 0 0 9 - 0 3 - 3 0

so, 0 to 4 is a year 5 to 6 is month 8 to 9 is day
0
 
t0t0Commented:
The date bits start with position number 0. So, the first two bits (on the left) are 0,2 - (that's interpretted as "the substring starting at character position 0, and which contains 2")

So, in the case of

   dd/mm/yyyy

'dd' will be 0,2 (or -10,2)
'mm' will be 3,2 (or -7,2)
'yyyy' will be 6,4 (or just -4)


If your date format is:

   dd/mm/yyyy

then to get:

   %yyyy%_%mm%_%dd%

you'll need the following code:

   set Pdate=%date:~-4%_%date:~3,2%_%date:~0,2%
0
 
t0t0Commented:
If your date format is:

   yyyy/mm/dd

then to get:

    %yyyy%_%mm%_%dd%

you simply need:

   set Pdate=%date:/=_%
0
 
gisvpnAuthor Commented:
Great thank you for the help ;)
0
 
sirbountyCommented:
"I'm glad also that my correct answer was not graded :)"

Roads_Roads, just so you are aware, the question was answered before you even posted your incorrect answer.

The format of the date variable is most certainly 'not' as you indicated:

0 1 2 3 4 5 6 7 8 9
----------------------
2 0 0 9 - 0 3 - 3 0

so, 0 to 4 is a year 5 to 6 is month 8 to 9 is day

In fact, if you interpret the author's original code, you'll be able to determine that their regional settings are MMDDYYYY (not YYYY-MM-DD).
t0t0 has correctly displayed the interpretation(s) presented by the working code.

If you type
echo %date%
at a command prompt, you'll see the format that the date is laid out as - mine is DDD MM/DD/YYYY
So, in order for me to set a variable to YYYY_MM_DD, I would need to start my new variable at position 4 and grab that and the next position (5) using:
Set MM=%date:~4,2%

DDD MM/DD/YYYY
0123456789012

The day is acquired by using the same format, beginning at character 7

Set DD=%date:~7,2%

Finally, the year can be acheived by beginning at position 10 (the second 0 above), and since we know it's 4 characters, it could be retrieved using:

Set YYYY=%date:~10,4%

However, it's simpler to just grab the last 4 characters using
Set YYYY=%date:~-4%

The negative sign (-) states to start 4 characters from the right side and retrieves all characters to the end, since we didn't specify how many to pulll...
0
 
t0t0Commented:
I didn't realise you had already awarded points for this question... never mind. You can also force the system to format the date to dd/mm/yyyy, do your stuff, then return the date format back to what it was, as in the following:

@echo off

rem specify date format
set datestring=dd/MM/yyyy

rem save current date format
for /f "tokens=3,3" %%a in ('reg query "hkcu\control panel\international" /v sshortdate') do (
   set sfmt=%%a
)

for /f "tokens=3,3" %%a in ('reg query "hkcu\control panel\international" /v slongdate') do (
   set lfmt=%%a
)

rem replace current date format with new date format
reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d %datestring% /f >nul
reg add "hkcu\control panel\international" /v slongdate /t reg_sz /d %datestring% /f >nul

rem get the date
set datestring=%date%

rem return original date format
reg add "hkcu\control panel\international" /v sshortdate /t reg_sz /d %sfmt% /f >nul
reg add "hkcu\control panel\international" /v slongdate /t reg_sz /d %lfmt% /f >nul

rem unset variables
set sfmt=
set lfmt=

rem date format as dd/mm/yyyy
echo %datestring%
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.