Formating Date

I'm trying to use echo %date% in  a batch file. however, it sets the format as 02/27/2018.  I'm trying to get rid of the 1st zero:
 set YYYY=%date:~-4,4%
 set MM=%date:~-3,2%
 set DD=%date:~-0,1%
 set /a D2 = %DD% -1

echo Todays date:  %date%
WellingtonISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Try this:

@echo off
setlocal

set YYYY=%date:~-4,4%
set MM=%date:~-7,2%
set DD=%date:~-10,2%
set /a DD=1%DD%-100

echo DATE : %date%
echo YYYY : %YYYY%
echo MM   : %MM%
echo DD   : %DD%

Open in new window


»bp
0
WellingtonISAuthor Commented:
That kind of gave me what I wanted.  But I need to use the date in a command for the day before...
xcopy \\169.123.190.19\N$\folder\folder\%yyyy%\%MM%\%d2% (that was minus 1) Y:\N\%yyyy%\%MM%\%d2% /v /f /s /i
0
Bill PrewCommented:
BAT files are pretty bad at date math.  The problem with "the date before" is that when it's day 1 of the month, then you have to move back to the last day of prior month, which means you have to have a table of days in each month, etc.  And then of course there is leap year.  And when you get to 1/1/xxxx you need to roll the year back as well.  It's doable, but messy.

I have some code if that's really what you need.  But I typically use a small VBS helper routine to do just the date math, it's a lot simpler approach.  Any interest in that?


»bp
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Bill PrewCommented:
For example, save this as a BAT and give it a try...  Happy to explain what's going on if it's not obvious, which it may not be...

@echo off
setlocal

echo Wscript.Echo Year(Date-1)^&" "^&Month(Date-1)^&" "^&Day(Date-1)>"%temp%\dateparts.vbs"
for /F "tokens=1-3" %%A in ('cscript //nologo "%temp%\dateparts.vbs"') do (
  set CurYr=%%A
  set CurMo=%%B
  set CurDay=%%C
)

echo CurYr=%CurYr%
echo CurMo=%CurMo%
echo CurDay=%CurDay%

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WellingtonISAuthor Commented:
so replace the %yyyy% variable with %CurYr%? Etc...
0
Bill PrewCommented:
Right, those are just the variables I had assigned the month, day and year of "yesterday" to in a sample I had handy, you can make them whatever you want.


»bp
0
WellingtonISAuthor Commented:
I think that did it!  I will not know until tomorrow. thanks so much.  I've been struggling with this for weeks!
0
Bill PrewCommented:
Great, glad that helped.


»bp
0
WellingtonISAuthor Commented:
Thanks much! That worked great
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

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.