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 PrewIT / Software Engineering ConsultantCommented:
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
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
Bill PrewIT / Software Engineering ConsultantCommented:
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
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Bill PrewIT / Software Engineering ConsultantCommented:
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

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...
Bill PrewIT / Software Engineering ConsultantCommented:
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
WellingtonISAuthor Commented:
I think that did it!  I will not know until tomorrow. thanks so much.  I've been struggling with this for weeks!
Bill PrewIT / Software Engineering ConsultantCommented:
Great, glad that helped.


»bp
WellingtonISAuthor Commented:
Thanks much! That worked great
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.