Link to home
Start Free TrialLog in
Avatar of WellingtonIS
WellingtonIS

asked on

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%
Avatar of Bill Prew
Bill Prew

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
Avatar of WellingtonIS

ASKER

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
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
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
so replace the %yyyy% variable with %CurYr%? Etc...
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
I think that did it!  I will not know until tomorrow. thanks so much.  I've been struggling with this for weeks!
Great, glad that helped.


»bp
Thanks much! That worked great