As I already said, a perpetual calendar doesn't mean much
Who is going to look up the weekday of year 1 AD?
Back then there wasn't even the Gregorian calendar
If you really want to make a professional chronology
then you need to consider BC as well
Who knows whether this formula is still applicable in BC
And your code has already added a 0~55 judgment
That means you are also defaulting 0~55 to +2000
----------------------------------------------------
After simplifying the date formatting and validation code, it is as follows
:: Monthly calendar query tool China DOS Union special edition. Please indicate copyright when reposting
:: Original: zjl5 Updated: namejm, qzwqzw 2007-06-03
:: Algorithm: Kim Larsen calculation formula
:: W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
:: Treat January and February as the 13th and 14th months of the previous year
:: Example: if it is 2004-1-10 then convert it to: 2003-13-10 and substitute it into the formula.
:: The input date format is: year-month-day (- can be replaced with : or /, and they can be mixed)
:: In the calendar, ★=today
:: Supports multiple date input formats:
:: ① If only one number is entered, it is treated as querying the month of the current year; the last two digits are automatically taken for the query, and ★ is marked on the 1st;
:: ② If two numbers are entered, it is treated as querying the year and month, and ★ is marked on the 1st;
:: ③ If the full date is entered, ★ is marked on the specified date
:: About year conversion:
:: ① If the year entered has fewer than three digits, it is converted as follows:
:: 50~99 is judged as 19xx
:: 0~49 is judged as 20xx
:: ② If the entered year has more than two digits, then take the last four characters (pad the high end with 0 if needed),
:: and calculate the date of that year according to rule ①;
@echo off
color 3F
mode con cols=40 lines=20
setlocal enabledelayedexpansion
set str=SunMonTueWedThuFriSat
set sdate=%date%
:Main
cls&echo.
:: Date extraction, formatting, and validation
for /f "tokens=1,2,3 delims=-/: " %%i in ("%sdate%") do (
(set sy=%%i) && (set sm=%%j) && (set sd=%%k)
)
if not defined sd set sd=1
if not defined sm set sm=%sy%& set sy=%date:~0,4%
(set sy=0000%sy%) && (set sm=00%sm%) && (set sd=00%sd%)
(set sy=%sy:~-4%) && (set sm=%sm:~-2%) && (set sd=%sd:~-2%)
cd.
set /a y=1%sy%-10000, m=1%sm%-100, d=1%sd%-100 2>nul
if errorlevel 1 goto Error
if %y% lss 100 (
if %y% lss 50 (set /a y+=2000) else (set /a y+=1900)
set sy=!y!
)
if %m% lss 13 if %d% lss 32 goto Calc
:Error
echo.Invalid date.
pause>nul
set sdate=%date%
goto Main
:Calc
:: Calculate the number of days in each month
set days=31
for %%i in (4 6 9 11) do if %m% equ %%i set days=30
:: Calculate February adjustment
set /a leap="^!(y%%4) & ^!(^!(y%%100)) | ^!(y%%400)"
if %m% equ 2 set /a days=28+%leap%
if %m% leq 2 (set /a y-=1& set /a m+=12)
:: Calculate the weekday of the specified date
set /a w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%%7
echo. %sy%-%sm% Query date:%sy%-%sm%-%sd%,week !str:~%w%,1!
echo.
:: Generate the monthly calendar
set /a wb=(w+35-d) %% 7, we=wb+days+1, day=1
echo. Sun Mon Tue Wed Thu Fri Sat
echo. ━━━━━━━━━━━━━━━━━━━
set /p= <nul
for /l %%i in (0,1,37) do (
set "temp= "
if %%i GTR %wb% if %%i LSS %we% (
set temp= !day!
set temp=!temp:~-2!
if !d! EQU !day! set temp=★
set /a day+=1
)
set /p= !temp!<nul
set /a "wm=(%%i+1)%%7"
if !wm! equ 0 echo.&echo.&set /p= <nul
)
echo.
echo ━━━━━━━━━━━━━━━━━━━
echo. Enter a date to query the weekday and display that month's calendar
echo.
set sdate=
set /p sdate= Format like: 07-02-03, Exit:
if defined sdate goto Main
[
Last edited by qzwqzw on 2007-8-5 at 06:48 PM ]