Link to home
Start Free TrialLog in
Avatar of NAPMA IT Ops
NAPMA IT OpsFlag for Netherlands

asked on

How to create a batch file with a date format YYYY-MM-DD?

The current output of my backup script is like "DailyBackup-2010Feb04"
I use the following inputs to convert date.

set MyDate=%date:~-4%%date:~3,3%%date:~0,2%
"....DailyBackup-(%MyDate%).bkf"

But I want to stamp the date like "YYYY-MM-DD"

How can I achieve this without changing my system's short date format?

Thanks.
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

You should just need to modify the characters used in the set command.  IF you post the output of the following command, I should be able to post what you need:
echo %date%

If you want to try yourself, then understand what the Set MyDate= line is doing:
%date:~-4% is saying to take the LAST FOUR (-4) characters from the output of %date% (which in your current setup is equal to "2010")
The %date:~3,3% is saying to start at the 3rd character of the %date% value and take 3 characters from there (which in your current setup is equal to "Feb")
The %date:~0,2% is saying to start at the 0 character of the %date% value and take the next 2 characters from there (which is the day - 04).

Using the US date format, I get the following:
echo %date%
Thu 02/04/2010
So if I wanted to turn that into YYYY-MM-DD
I'd take the last 4, the 2 characters starting at the 4th character, and the 2 characters starting at the 7th character (and put in "-" to separate each):
Set MyDate=%date:~-4%-%date:~4,2%-%date:~7,2%
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you saying my comments were of NO HELP to you?
Avatar of NAPMA IT Ops

ASKER

Dear Expert Billprew,

With your code I can have the date the way I want. But instead of "2010 -02  -05", I want "2010-02-05"
Can you please tell me how I can get rid of spaces in between  dates? Thanks...
Dear Expert Leew,

I am saying that your comment did not work for me!.. But your code outputs the date like 2010Feb05. I want to have the month in numeric like 2010-02-05!... Thanks
Avatar of Bill Prew
Bill Prew

==> With your code I can have the date the way I want. But instead of "2010 -02  -05",
==> I want "2010-02-05".  Can you please tell me how I can get rid of spaces in between  dates?

That's a dumb Experts Exchange formatting problem.  It often adds an extra blank space to the end of each line when posting attached "Code" to a reply.  Check your BAT file and remove any trailing spaces from each line.

Another way to work around this is to use some quoting on the SET commands, so I think the attached would solve the problem as well.

~bp

@echo off
for /f "skip=2 tokens=2-4 delims=," %%A in ('WMIC Path Win32_LocalTime Get Day^,Month^,Year /Format:csv') do (
  set "dd=%%A"
  set "mm=%%B"
  set "yyyy=%%C"
)
if %mm% LSS 10 set "mm=0%mm%"
if %dd% LSS 10 set "dd=0%dd%"
echo %yyyy%-%mm%-%dd%

Open in new window

PEITO - if you READ my comment, I tried to explain how YOU could LEARN to do it yourself - I tried to HELP YOU UNDERSTAND what exactly you were doing.  Are you saying you'd prefer to be ignorant about what you're doing?  If that's so, one day someone is going to give you code that damages your computer and you're going to blindly run it and ultimately be responsible.

I ALSO asked you to post the results of the command:
echo %date%
Where did you do so?

Your date format is different from my and I would be happy to help you - IF you respond to my original comment.  The amusing thing about this, if YOU had take 10 seconds to do what I ask and post back, you'd have an answer that worked right now...