Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subtracting date in bat file

Tags:

batch-file

cmd

I am trying to get a start date and an end date. The start and end data should vary by 1 full day. I am having issue subtracting or adding from either the start or end date. How can I go about doing this? I have attempted -%1% and +%1%.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"

set /a y=%dt:~0,4%
set /a m=1%dt:~4,2%
set /a d=1%dt:~6,2%


set subdate=%y%%m:~-2%%d:~-2%

SET START_DATE= %subdate:~0,4%/%subdate:~4,2%/%subdate:~6,2% 00:00 AM
SET END_DATE = %dt:~0,4%/%dt:~4,2%/%dt:~6,2%+%1% 00:00 AM

echo %start_date%
echo %end_date%
echo done
like image 746
Brad Avatar asked Dec 06 '25 09:12

Brad


1 Answers

Here is a way to get start date to be yesterday and end date to be today. If I misunderstand what you are seeking, please say so.

FOR /F "usebackq tokens=*" %%t IN (`powershell -NoProfile -Command "(Get-Date).AddDays(-1).ToString('yyyy/MM/dd HH:mm:ss')"`) DO (SET "START_DATE=%%t")
FOR /F "usebackq tokens=*" %%t IN (`powershell -NoProfile -Command "(Get-Date).ToString('yyyy/MM/dd HH:mm:ss')"`) DO (SET "END_DATE=%%t")

echo %START_DATE%
echo %END_DATE%
like image 194
lit Avatar answered Dec 09 '25 18:12

lit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!