我也来发一个农历转换函数,查表法做的,支持1921-2021年。
应该无误差,除非日期如 2月31这种。- @ECHO OFF&SETLOCAL ENABLEDELAYEDEXPANSION
- ::info 包含了1921-2021年的农历数据,每个数字的前11-12位包含大小月情况,某位为0表示某月29天,为1表示30天。11位,即<4095,表示无闰月,12位以上表示有闰月。
- ::然后根据 阳历日期到1921-2-8的天数差,一个一个减去阴历天数,直到 剩余天数不足一个月时,退出循环。
- ::再根据 把info ">>" 16位,从16位开始记录的是闰几月。
- ::忘记了一个小错误,“十一”月是2个字符,改程序麻烦,改成为“⑾”,最后再替换。
-
-
-
- call :getNongLi 2009 5 24 日期
- set "er=!ERRORLEVEL!"
- echo,日历: !日期!
- echo,农历数字,方便查节日: !er!
- goto :eof
-
-
-
-
-
-
-
- :getNongLi [year] [month] [day] [var_Name] (函数返回ERRORLEVEL=农历日期数字,值为负表示当月为闰月,0为超标或者错误日期)
- SETLOCAL ENABLEDELAYEDEXPANSION
- ::检测传入
- set "dt=%date:~0,10%"&set "temp=%1-%2-%3"
- set /a $1=%1-0,$2=%2-0,$3=%3-0
- 2>nul set /a 1/(%1%2%3) && (
- if !$1! gtr 2021 if !$1! lss 1921 if !$2! gtr 12 if !$2! lss 1 if !$3! gtr 31 if !$3! lss 1 exit /b 0
- if "%4"=="" (set var_name=nonglidays) else set "var_name=%4"
- ) || (
- if "%1"=="" (set var_name=nonglidays) else set "var_name=%1"
- set temp=!dt!
- )
- ::初始变量
- set "info=2635,333387,1701,1748,267701,694,2391,133423,1175,396438,3402,3749,331177,1453,694,201326,2350,465197,3221,3402,400202,2901,1386,267611,605,2349,137515,2709,464533,1738,2901,330421,1242,2651,199255,1323,529706,3733,1706,398762,2741,1206,267438,2647,1318,204070,3477,461653,1386,2413,330077,1197,2637,268877,3365,531109,2900,2922,398042,2395,1179,267415,2635,661067,1701,1748,398772,2742,2391,330031,1175,1611,200010,3749,527717,1452,2742,332397,2350,3222,268949,3402,3493,133973,1386,464219,605,2349,334123,2709,2890,267946,2773,592565,1210,2651,395863,1323,2707,265877"
- set "weeks=日一二三四五六"
- set "天干表=甲乙丙丁戊己庚辛壬癸"
- set "地支表=子丑寅卯辰巳午未申酉戌亥"
- set "生肖表=鼠牛虎兔龙蛇马羊猴鸡狗猪"
- set "农历日期表=初一初二初三初四初五初六初七初八初九初十十一十二十三十四十五十六十七十八十九二十廿一廿二廿三廿四廿五廿六廿七廿八廿九三十"
- set "农历月份表=正二三四五六七八九十⑾腊"
- set "公历月天数=0,31,59,90,120,151,181,212,243,273,304,334"
- for %%i in (%info%) do set /a num+=1&set "info_!num!=%%i"
- ::处理日期,如果有误,请根据自己的日期格式修改。
- set temp=%temp:-0=-%
- for /f "tokens=1,2,3 delims=- " %%1 in ("%temp%") do set /a curYear=%%1,curMonth=%%2,curDay=%%3
- set "公历日期=!curYear!年!curMonth!月!curDay!日"
- ::获取公历星期 Ritchie Lawrence日期函数
- setlocal
- set /a z=14-curMonth,z/=12,y=curYear+4800-z,m=curMonth+12*z-3,dow=153*curMonth+2
- set /a dow=dow/5+curDay+y*365+y/4-y/100+y/400-2472630,dow%%=7
- endlocal&set week=%dow%
- for %%i in (!week!) do set 公历日期=!公历日期! 星期!weeks:~%%i,1!
- ::计算到初始时间1921年2月8日的天数:1921-2-8(正月初一) 最多到 2021年
- set temp=for /f "tokens=!curMonth! delims=," %%i in ("%公历月天数%") do set "MonthAdd=%%i"
- %temp%
- set /a TheDate= (curYear - 1921) * 365 + (curYear - 1921) / 4 + curDay + MonthAdd - 38
- if !curMonth! gtr 2 (
- set/a temp=curYear %% 4
- if !temp! equ 0 set /a TheDate+=1
- )
- ::计算农历天干、地支、月、日
- set /a isEnd=0,m=0
- for %%m in (%info%) do (
- if !isEnd! equ 0 (
- if %%m lss 4095 ( set k=11 ) else ( set k=12 )
- set /a n=k
- for /l %%n in (!n!,-1,0) do (
- if !isEnd! equ 0 (
- set /a bit=%%m,n=%%n,bit=bit">>"n,bit=bit %% 2,temp=bit+29
- if !TheDate! leq !temp! set /a isEnd=1
- if !isEnd! equ 0 set /a TheDate= TheDate - 29 - bit
- ))
- set /a m+=1
- if !isEnd! equ 1 set /a m-=1
- ))
- set /a curYear=1921 + m,curMonth=k - n + 1,curDay=TheDate
- If !k! equ 12 (
- set /a temp=info_!m! / 65536 + 1
- if !curMonth! equ !temp! set /a curMonth= 1 - curMonth
- if !curMonth! gtr !temp! set /a curMonth-=1
- )
- ::生成农历天干、地支、属相
- set /a tof=((curYear - 4) %% 60) %% 10,dof=((curYear - 4) %% 60) %% 12
- for /f "tokens=1,2" %%a in ("!tof! !dof!") do set 农历日期=农历!天干表:~%%a,1!!地支表:~%%b,1!年(!生肖表:~%%b,1!)
- ::生成农历月、日
- set /a cof=curMonth-1,tof=(curMonth-1) * -1,dof=(curDay -1) * 2,ecode=curYear*10000 + curMonth * 100 + curDay
- for /f "tokens=1,2,3" %%a in ("!tof! !cof! !dof!") do (
- If !curMonth! lss 1 (
- set 农历日期=!农历日期! 闰!农历月份表:~%%a,1!月!农历日期表:~%%c,2!
- set /a ecode*=-1
- ) Else (
- set 农历日期=!农历日期! !农历月份表:~%%b,1!月!农历日期表:~%%c,2!
- ))
- set "农历日期=!农历日期:⑾=十一!"
- endlocal&set "%var_name%=%公历日期% %农历日期%" & exit /b %ecode%
复制代码
|