views:

115

answers:

3

I just want to figure out,,

How can a batch file (CMD) tell me how many days i'm left from any date...???

Such as:

set setdate=24/07/2009
echo The date is now: %date%
echo Your set date is: %setdate%
echo You're %days% days left from %setdate%

Thanks!

+1  A: 

It might be possible to achieve that with the simple math functionality in cmd.exe, but it would depend on the regional settings of the machine (if you use a date format of DD/MM/YYYY, it will break on my YYYY-MM-DD), and it would take a lot of code.

It probably would be much easier to just use a scripting language such as Perl, PHP or Python for that.

grawity
Well, i will fix that,, but can you give me the code for it.
YourComputerHelpZ
+3  A: 

...okay, since the asker wants me to do all the work for him, here's the script:

@echo off & setlocal
set now=%date%
set then=2010-08-01

:: split into year, month, day
set nY=%now:~0,4%&set nM=%now:~5,2%&set nD=%now:~8,2%
set tY=%then:~0,4%&set tM=%then:~5,2%&set tD=%then:~8,2%

:: remove leading zero
if "%nM:~,1%"=="0" set nM=%nM:~1%
if "%tM:~,1%"=="0" set tM=%tM:~1%
if "%nD:~,1%"=="0" set nD=%nD:~1%
if "%tD:~,1%"=="0" set tD=%tD:~1%

set mdays=31 28 31 30 31 30 31 31 30 31 30 31
set n=0
set t=0
call :days n %nM% %mdays%
call :days t %tM% %mdays%

set /a d=(t+tD)-(n+nD) + ((tY-nY) * 365)

goto :skip
:days
set var=%1
set M=%2
set i=0
shift & shift
:days_loop
if %i% geq %M% goto :EOF
set /a i=i+1
set /a %var%=%var%+%1
shift & goto :days_loop
:skip

echo The difference between %now% and %then% is %d% days.

For comparison, the same thing in Python:

import datetime
now = datetime.datetime.now()
then = datetime.datetime(
    # Do NOT use leading zeros here.
    year = 2010,
    month = 8,
    day = 1,
)
diff = then - now
grawity
+1  A: 

Here's a simple solution in VBScript:

Dim dt:    dt    = DateValue(WScript.Arguments.Unnamed(0))
Dim dtNow: dtNow = Now
Dim diff:  diff  = DateDiff("d", dtNow, dt)

WScript.Echo "The date is now:  " & FormatDateTime(dtNow, vbShortDate)
WScript.Echo "Your set date is: " & FormatDateTime(dt, vbShortDate)
WScript.Echo "You're " & diff & " days left from " & FormatDateTime(dt, vbShortDate)

You can call this script from your batch file and specify the target date like this:

cscript filename.vbs 12/25/2009 //nologo
Helen
YourComputerHelpZ