Page 1 of 1
If date is older than 30 days
Posted: 08 May 2012 15:26
by MKANET
It's pretty easy for me to delete and/or find files that are older than a certain number of days since there are builtin commands for this.
However... I can't figure out how to determine if a date is not older than 30 days.
For example, using the follow date format: 01/08/2012
I would like the code to determine if the above date (or any date with that format) is not any older than 30 days
Thanks in advance!
Re: If date is older than 30 days
Posted: 08 May 2012 15:57
by aGerman
Calculate an ongoing number of days. The
date2jdate function is suitable.
Code: Select all
@echo off &setlocal
set "OldDate=01/08/2012"
for /f "tokens=1-3 delims=/" %%i in ("%OldDate%") do (
set "OldM=%%i"
set "OldD=%%j"
set "OldY=%%k"
)
call :date2jdate OldJD %OldY% %OldM% %OldD%
echo %OldJD%
pause
goto :eof
:date2jdate JD YYYY MM DD -- converts a gregorian calender date to julian day format
:: -- JD [out] - julian days
:: -- YYYY [in] - gregorian year, i.e. 2006
:: -- MM [in] - gregorian month, i.e. 12 for december
:: -- DD [in] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set "yy=%~2"&set "mm=%~3"&set "dd=%~4"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
Do the same with the current date and substract that JD value from the older.
Regards
aGerman
Re: If date is older than 30 days
Posted: 08 May 2012 16:03
by Squashman
If you are referring to file dates you could use Forfiles. Believe it works for older and newer with the /D switch.
/D +30
/D -30
Re: If date is older than 30 days
Posted: 08 May 2012 16:25
by MKANET
Hi aGerman, I'm not too sure if I understood you. I did what I think you suggested (below). However, I'm not getting a meaningful result:
Result:
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).
34Code: Select all
@echo off &setlocal
set "OldDate=01/08/2012"
for /f "tokens=1-3 delims=/" %%i in ("%OldDate%") do (
set "OldM=%%i"
set "OldD=%%j"
set "OldY=%%k"
)
call :date2jdate OldJD %OldY% %OldM% %OldD%
set "newDate=%date%"
for /f "tokens=1-3 delims=/" %%i in ("%newDate%") do (
set "newM=%%i"
set "newD=%%j"
set "newY=%%k"
)
call :date2jdate newJD %newY% %newM% %newD%
set /a days=oldJD-newJD
echo %days%
pause
goto :eof
:date2jdate JD YYYY MM DD -- converts a gregorian calender date to julian day format
:: -- JD [out] - julian days
:: -- YYYY [in] - gregorian year, i.e. 2006
:: -- MM [in] - gregorian month, i.e. 12 for december
:: -- DD [in] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set "yy=%~2"&set "mm=%~3"&set "dd=%~4"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
Re: If date is older than 30 days
Posted: 08 May 2012 16:36
by aGerman
... but that doesn't explain why the error occurs.
What's the output of
in your case?
Regards
aGerman
Re: If date is older than 30 days
Posted: 08 May 2012 17:33
by MKANET
Oops

I forgot that %date% output by default uses this format:
Tue 05/18/2012
Now... it works perfectly. Below is the output an respective code. Thanks as always aGerman!!
01/08/2012 is over 30 days ago...Code: Select all
@echo off &setlocal
set "OldDate=01/08/2012"
for /f "tokens=1-3 delims=/" %%i in ("%OldDate%") do (
set "OldM=%%i"
set "OldD=%%j"
set "OldY=%%k"
)
call :date2jdate OldJD %OldY% %OldM% %OldD%
for /f "tokens=2" %%a in ("%date%") do set date=%%a
set "newDate=%date%"
for /f "tokens=1-3 delims=/" %%i in ("%newDate%") do (
set "newM=%%i"
set "newD=%%j"
set "newY=%%k"
)
call :date2jdate newJD %newY% %newM% %newD%
set /a days=newJD-OldJD
if %days% GEQ 30 Echo %OldDate% is over 30 days ago...
pause
goto :eof
:date2jdate JD YYYY MM DD -- converts a gregorian calender date to julian day format
:: -- JD [out] - julian days
:: -- YYYY [in] - gregorian year, i.e. 2006
:: -- MM [in] - gregorian month, i.e. 12 for december
:: -- DD [in] - gregorian day, i.e. 31
:$reference http://aa.usno.navy.mil/faq/docs/JD_Formula.html
:$created 20060101 :$changed 20080219 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set "yy=%~2"&set "mm=%~3"&set "dd=%~4"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b