Oh my, you are new to this! Welcome to the world of Windows batch programming
Those functions I referred you to are just that. They are functions that must be called using the CALL command. You should read this
DOS Batch - Function Tutorial for general info about functions. The DOS tips web site demonstrates sophisticated batch programming techniques that you will not see in the average script! Don't worry if some of it doesn't make sense yet.
One of your best friends when learning batch programming (besides the DOS Tips site) will be the HELP command issued from an interactive command prompt. Learn how to use it.
In the mean time, here is a functioning program that demonstrates how to use the date functions.
Notes:I reverted the entered date format to your original spec to demonstrate how to use the FOR /F command to parse the values.
Tuesday computation: I used the SET /A command to add 1 to the Monday Julian date.
Wednesday computation: I let the :jdate2date function perform the addition! This is not normal behavior of functions. It only works because the :jdate2date function is using SET /A when initially reading the argument.
Sunday computation: I added spaces to the simple math formula to show how you can include spaces in a function argument by enclosing the argument in quotes. I also printed the date directly without building a Sun variable.
After the last computation I added the EXIT /B to ensure that the code does not fall through and attempt to execute the functions without a call.
Code: Select all
@echo off
setlocal
SET /P "MAIL_DATE=Enter the Monday maildate (MM/DD/YYYY): "
for /f "tokens=1-3 delims=/" %%a in ("%MAIL_DATE%") do (
set MonMM=%%a
set MonDD=%%b
set MonYYYY=%%c
)
echo Monday Date=%MAIL_DATE%
set mon
call :date2jdate MonJD %MonYYYY% %MonMM% %MonDD%
echo Julian date=%MonJD%
REM get Tuesday date
set /a TueJD=MonJD + 1
call :jdate2date TueJD TueYYYY TueMM TueDD
set Tue=%TueMM%/%TueDD%/%TueYYYY%
echo Following Tue=%Tue%
REM get Wednesday date
call :jdate2date (MonJD+2) WedYYYY WedMM WedDD
set Wed=%WedMM%/%WedDD%/%WedYYYY%
echo Following Wed=%Wed%
REM get Sunday date
call :jdate2date "(MonJD + 6)" SunYYYY SunMM SunDD
echo Following Sun=%SunMM%/%SunDD%/%SunYYYY%
pause
exit /b
: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
:jdate2date JD YYYY MM DD -- converts julian days to gregorian date format
:: -- JD [in] - julian days
:: -- YYYY [out] - gregorian year, i.e. 2006
:: -- MM [out] - gregorian month, i.e. 12 for december
:: -- DD [out] - 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 ENABLEDELAYEDEXPANSION
set /a L= %~1+68569, N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447, K= L-2447*J/80, L= J/11
set /a J= J+2-12*L, I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2%
set DD=%DD:~-2%
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
EXIT /b
Sample output
Code: Select all
Enter the Monday maildate (MM/DD/YYYY): 5/30/2011
Monday Date=5/30/2011
MonDD=30
MonMM=5
MonYYYY=2011
Julian date=2455712
Following Tue=05/31/2011
Following Wed=06/01/2011
Following Sun=06/05/2011
Press any key to continue . . .
Addendum - you can validate the MAILDATE is a Monday by using the modulo (%) operator. Julian date modulo 7 = 0 for Monday, 1 for Tuesday, etc.Dave Benham