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%
set YYYY=%date:~-4,4%
set MM=%date:~-3,2%
set DD=%date:~-0,1%
set /a D2 = %DD% -1
echo Todays date: %date%
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\%y yyy%\%MM%\ %d2% (that was minus 1) Y:\N\%yyyy%\%MM%\%d2% /v /f /s /i
xcopy \\169.123.190.19\N$\folder
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
»bp
ASKER
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
»bp
ASKER
Thanks much! That worked great
Open in new window
»bp