Solved

Batch File Date

Posted on 2009-03-30
15
485 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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