Solved

Batch File Date

Posted on 2009-03-30
15
488 Views
Last Modified: 2013-11-05
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

0
Comment
Question by:gisvpn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 24017852
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
 

Author Comment

by:gisvpn
ID: 24017865
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 24017881
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 24017898
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24018316
Please try the following:

set Pdate=%date:~-4%_%date:~3,2%_%date:~0,2%
0
 

Author Closing Comment

by:gisvpn
ID: 31564279
Excellent thank you.
0
 

Author Comment

by:gisvpn
ID: 24018410
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
 

Author Comment

by:gisvpn
ID: 24018414
what are the difference between all the posts ?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24018441
Glad I could help - thanx for the grade! :^)
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 24018494
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24018496
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
 
LVL 16

Expert Comment

by:t0t0
ID: 24018514
If your date format is:

   yyyy/mm/dd

then to get:

    %yyyy%_%mm%_%dd%

you simply need:

   set Pdate=%date:/=_%
0
 

Author Comment

by:gisvpn
ID: 24018619
Great thank you for the help ;)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24018635
"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
 
LVL 16

Expert Comment

by:t0t0
ID: 24018751
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Batch file output 20 95
Reusing a former RAID Member as a data drive 18 181
how to diff two files 12 56
Copy file to another folder as soon as it hits the first folder 6 31
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question