3

could someone help as I would like to check and compare a date added to the registry via a .bat file. I am inserting the date into the registry using:

REG ADD HKCU\software\MMG /v datestamp /d "%DATE%" /t REG_EXPAND_SZ /

This is shown as 01/12/2013

Basically I am trying to check that date and if its 30days old process a function within the .bat file.

Thanks in advance

DaE
  • 189
  • 1
  • 4
  • 14
  • possible duplicate of [Compare 2 dates in a Windows batch file](http://stackoverflow.com/questions/17649235/compare-2-dates-in-a-windows-batch-file) – shf301 Dec 01 '13 at 21:49
  • in your opinion. however, there is a rich set of functions on dostips.com that make it VERY easy. so please refrain from saying that any language is a poor choice. maybe someone is using batch for fun. maybe they only know batch, and it takes them 2 years to learn a language. maybe they can only use what is native. you never know. – cure Dec 01 '13 at 23:08
  • to the op: could you please show an attempt so we have something to go off of? thanks – cure Dec 01 '13 at 23:09
  • This is only a small project at home and am currently learning myself this language. It's a date extract from the registry and compare with the latest date. Thanks for your input – DaE Dec 01 '13 at 23:56
  • @nephi12 I'm not saying it's not possible, but for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do ( for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do ( set %%a=%%A&set %%b=%%B&set %%c=%%C)) is not exactly a language designed for date manipulation. (The code also assumes US English.) – Raymond Chen Dec 02 '13 at 00:56

1 Answers1

2

this is for British date format 'dd/mm/yyyy', echo %date% output 02/12/2013:

@ECHO OFF &SETLOCAL
rem REG ADD HKCU\software\MMG /v datestamp /d "01/11/2013"
for /f "tokens=2*" %%a in ('reg query HKCU\software\MMG /v datestamp') do set "RegDATE=%%b"
call:DateToJDN %RegDATE% RegJDN
call:DateToJDN %DATE% JDN
set /a diff=JDN-RegJDN
echo(%Diff% days between %RegDATE% and %DATE%.
goto:eof

rem Convert the date to Julian Day Number
:DateToJDN dd/mm/yyyy jdn=
setlocal
set date=%1
set /A yy=%date:~-4%, mm=1%date:~-7,2% %% 100, dd=1%date:~-10,2% %% 100
set /A a=mm-14, jdn=(1461*(yy+4800+a/12))/4+(367*(mm-2-12*(a/12)))/12-(3*((yy+4900+a/12)/100))/4+dd-32075
endlocal & set %2=%jdn%
exit /B

Output:

31 days between 01/11/2013 and 02/12/2013.
Endoro
  • 37,015
  • 8
  • 50
  • 63