網(wǎng)上流傳很多計(jì)算公農(nóng)歷的源代碼,很多,但是居然沒有vb的,暈, 
所以。。。。。 
 
 用法: 
 以l開始的方法均為陰歷,以s開始的方法均為公歷 
 基本的兩個(gè)初使函數(shù): 
 linitdate:用農(nóng)歷年月日初使化日期對象 
 sinitdate: 用公歷年月日初使化日期對象 
 
 其它的方法看下面的一小段代碼吧 
示例代碼 
private sub command1_click() 
 dim t as clsdate 
 dim y as long 
 dim m as long 
 dim d as long 
 dim st as single 
 dim et as single 
 dim da as date 
 dim j as long 
 dim ret as long 
 set t = new clsdate 
 't.sinitdate 1900, 1, 1 
 t.linitdate 2047, 5, 12, false '農(nóng)歷2047年5月12日,非閏月 
 debug.print t.lyear 
 if t.isleap = false then 
 debug.print t.lmonth 
 else 
 debug.print " 閏 " & t.lmonth 
 end if 
 debug.print t.cdaystr(t.lday) '農(nóng)歷日期中文大寫 
 debug.print t.ganzhi(t.lyear) '求干支 
 debug.print t.yearattribute(t.lyear) '農(nóng)歷年的屬相 
 debug.print t.syear ' 公歷年 
 debug.print t.smonth ' 公歷月 
 debug.print t.sday ' 公歷日 
 debug.print t.sweekday '公歷星期 
 debug.print t.era(t.syear)' 公歷紀(jì)元 
 debug.print t.constellation(t.smonth, t.sday) ' 星座 
 debug.print "week:" & t.wholiday ' 按第幾個(gè)星期幾計(jì)算的假日 
 debug.print "solar" & t.sholiday ' 按公歷計(jì)算的假日 
 debug.print "lunar" & t.lholiday ' 按陰歷計(jì)算的假日 
 debug.print t.lsolarterm ' 計(jì)算節(jié)氣 
 
 '以下為速度測試,很快吧。 
 st = timer 
 with t 
 for y = 1900 to 2049 
 for m = 1 to 12 
 for d = 1 to 28 
 .linitdate y, m, d, false 
 
 next 
 next 
 next 
 end with 
 't.printf 
 et = timer 
 debug.print et - st 
 set t = nothing 
end sub 
 
 以下為代碼:
option explicit 
private type solarholidaystruct 
 month as long 
 day as long 
 recess as long 
 holidayname as string 
end type 
private type lunarholidaystruct 
 month as long 
 day as long 
 recess as long 
 holidayname as string 
end type 
private type weekholidaystruct 
 month as long 
 weekatmonth as long 
 weekday as long 
 holidayname as string 
end type 
'保持屬性值的局部變量 
private mvarsyear as long '局部復(fù)制 
private mvarsmonth as long '局部復(fù)制 
private mvarsday as long '局部復(fù)制 
private mvarlyear as long '局部復(fù)制 
private mvarlmonth as long '局部復(fù)制 
private mvarlday as long '局部復(fù)制 
private mvarisleap as boolean '局部復(fù)制 
private declare function bitright32 lib "bit4vb.dll" (byval x as long, byval num as long) as long 
'private declare function bitright16 lib "bit4vb.dll" (byval x as integer, byval num as integer) as integer 
'定義類內(nèi)部用公用變量 
private solarmonth as variant 
private gan as variant 
private zhi as variant 
private animals as variant 
private solarterm as variant 
private sterminfo as variant 
private nstr1 as variant 
private nstr2 as variant 
private monthname as variant 
private lunarinfo(150) as long 
private lunaryeardays(150) as long 
private sholidayinfo() as solarholidaystruct 
private lholidayinfo() as lunarholidaystruct 
private wholidayinfo() as weekholidaystruct 
private mvardate as date '內(nèi)部使用標(biāo)準(zhǔn)的日期變量 
private sub class_initialize() 
 dim temparray as variant 
 dim i as long 
 dim b as long 
 dim sftv as variant 
 dim lftv as variant 
 dim wftv as variant 
 '根據(jù)vb的位計(jì)算特點(diǎn),故擴(kuò)充原有的數(shù)據(jù)位,將其變成32位 
 temparray = array( _ 
 &h104bd8, &h104ae0, &h10a570, &h1054d5, &h10d260, &h10d950, &h116554, &h1056a0, &h109ad0, &h1055d2, _ 
 &h104ae0, &h10a5b6, &h10a4d0, &h10d250, &h11d255, &h10b540, &h10d6a0, &h10ada2, &h1095b0, &h114977, _ 
 &h104970, &h10a4b0, &h10b4b5, &h106a50, &h106d40, &h11ab54, &h102b60, &h109570, &h1052f2, &h104970, _ 
 &h106566, &h10d4a0, &h10ea50, &h106e95, &h105ad0, &h102b60, &h1186e3, &h1092e0, &h11c8d7, &h10c950, _ 
 &h10d4a0, &h11d8a6, &h10b550, &h1056a0, &h11a5b4, &h1025d0, &h1092d0, &h10d2b2, &h10a950, &h10b557, _ 
 &h106ca0, &h10b550, &h115355, &h104da0, &h10a5d0, &h114573, &h1052d0, &h10a9a8, &h10e950, &h106aa0, _ 
 &h10aea6, &h10ab50, &h104b60, &h10aae4, &h10a570, &h105260, &h10f263, &h10d950, &h105b57, &h1056a0, _ 
 &h1096d0, &h104dd5, &h104ad0, &h10a4d0, &h10d4d4, &h10d250, &h10d558, &h10b540, &h10b5a0, &h1195a6, _ 
 &h1095b0, &h1049b0, &h10a974, &h10a4b0, &h10b27a, &h106a50, &h106d40, &h10af46, &h10ab60, &h109570, _ 
 &h104af5, &h104970, &h1064b0, &h1074a3, &h10ea50, &h106b58, &h1055c0, &h10ab60, &h1096d5, &h1092e0, _ 
 &h10c960, &h10d954, &h10d4a0, &h10da50, &h107552, &h1056a0, &h10abb7, &h1025d0, &h1092d0, &h10cab5, _ 
 &h10a950, &h10b4a0, &h10baa4, &h10ad50, &h1055d9, &h104ba0, &h10a5b0, &h115176, &h1052b0, &h10a930, _ 
 &h107954, &h106aa0, &h10ad50, &h105b52, &h104b60, &h10a6e6, &h10a4e0, &h10d260, &h10ea65, &h10d530, _ 
 &h105aa0, &h1076a3, &h1096d0, &h104bd7, &h104ad0, &h10a4d0, &h11d0b6, &h10d250, &h10d520, &h10dd45, _ 
 &h10b5a0, &h1056d0, &h1055b2, &h1049b0, &h10a577, &h10a4b0, &h10aa50, &h11b255, &h106d20, &h10ada0) 
 for i = 0 to 149 
 lunarinfo(i) = temparray(i) 
 next 
 
 temparray = array( _ 
 384, 354, 355, 383, 354, 355, 384, 354, 355, 384, _ 
 354, 384, 354, 354, 384, 354, 355, 384, 355, 384, _ 
 354, 354, 384, 354, 354, 385, 354, 355, 384, 354, _ 
 383, 354, 355, 384, 355, 354, 384, 354, 384, 354, _ 
 354, 384, 355, 354, 385, 354, 354, 384, 354, 384, _ 
 354, 355, 384, 354, 355, 384, 354, 383, 355, 354, _ 
 384, 355, 354, 384, 355, 353, 384, 355, 384, 354, _ 
 355, 384, 354, 354, 384, 354, 384, 354, 355, 384, _ 
 355, 354, 384, 354, 384, 354, 354, 384, 355, 355, _ 
 384, 354, 354, 383, 355, 384, 354, 355, 384, 354, _ 
 354, 384, 354, 355, 384, 354, 385, 354, 354, 384, _ 
 354, 354, 384, 355, 384, 354, 355, 384, 354, 354, _ 
 384, 354, 355, 384, 354, 384, 354, 354, 384, 355, _ 
 354, 384, 355, 384, 354, 354, 384, 354, 354, 384, _ 
 355, 355, 384, 354, 384, 354, 354, 384, 354, 355) 
 
 for i = 0 to 149 
 lunaryeardays(i) = temparray(i) 
 next 
 
 solarmonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) 
 gan = array("甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸") 
 zhi = array("子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥") 
 animals = array("鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬") 
 solarterm = array("小寒", "大寒", "立春", "雨水", "驚蟄", "春分", "清明", "谷雨", "立夏", "小滿", "芒種", "夏至", "小暑", "大暑", "立秋", "處暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪", "冬至") 
 sterminfo = array(0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758) 
 nstr1 = array("日", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十") 
 nstr2 = array("初", "十", "廿", "卅", " ") 
 monthname = array("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec") 
 
 '國歷節(jié)日 *表示放假日 
 sftv = array( _ 
 1, 1, 1, "元旦", _ 
 2, 14, 0, "情人節(jié)", 2, 10, 0, "國際氣象節(jié)", _ 
 3, 18, 0, "婦女節(jié)", 3, 12, 0, "植樹節(jié)", 3, 15, 0, "消費(fèi)者權(quán)益日", _ 
 4, 1, 0, "愚人節(jié)", _ 
 5, 1, 1, "勞動(dòng)節(jié)", 5, 4, 0, "青年節(jié)", 5, 12, 0, "護(hù)士節(jié)", 5, 31, 0, "世界無煙日", _ 
 6, 1, 0, "兒童節(jié)", _ 
 7, 1, 0, "建黨節(jié) 香港回歸紀(jì)念", _ 
 8, 1, 0, "建軍節(jié)", 8, 8, 0, "中國男子節(jié) 父親節(jié)", _ 
 9, 9, 0, "毛澤東逝世紀(jì)念", 9, 10, 0, "教師節(jié)", 9, 18, 0, "九·一八事變紀(jì)念日", 9, 28, 0, "孔子誕辰", _ 
 10, 1, 0, "國慶節(jié) 國際音樂日", 10, 6, 0, "老人節(jié)", 10, 24, 0, "聯(lián)合國日", _ 
 11, 12, 0, "孫中山誕辰紀(jì)念", _ 
 12, 1, 0, "世界艾滋病日", 12, 3, 0, "世界殘疾人日", 12, 20, 0, "澳門回歸紀(jì)念", 12, 24, 0, "平安夜", 12, 25, 0, "圣誕節(jié)", 12, 26, 0, "毛澤東誕辰紀(jì)念") 
 
 b = ubound(sftv) + 1 
 redim sholidayinfo(b / 4) 
 for i = 0 to (b / 4) - 1 
 sholidayinfo(i).month = sftv(i * 4) 
 sholidayinfo(i).day = sftv(i * 4 + 1) 
 sholidayinfo(i).recess = sftv(i * 4 + 2) 
 sholidayinfo(i).holidayname = sftv(i * 4 + 3) 
 next 
 
 '農(nóng)歷節(jié)日 *表示放假日 
 lftv = array( _ 
 1, 1, 1, "春節(jié)", _ 
 1, 15, 0, "元宵節(jié)", _ 
 5, 5, 0, "端午節(jié)", _ 
 7, 7, 0, "七夕情人節(jié)", _ 
 7, 15, 0, "中元節(jié) 盂蘭盆節(jié)", _ 
 8, 15, 0, "中秋節(jié)", _ 
 9, 9, 0, "重陽節(jié)", _ 
 12, 8, 0, "臘八節(jié)", _ 
 12, 24, 0, "小年") 
 '12, 31, 0, "除夕") '注意除夕需要其它方法進(jìn)行計(jì)算 
 
 b = ubound(lftv) + 1 
 redim lholidayinfo(b / 4) 
 for i = 0 to (b / 4) - 1 
 lholidayinfo(i).month = lftv(i * 4) 
 lholidayinfo(i).day = lftv(i * 4 + 1) 
 lholidayinfo(i).recess = lftv(i * 4 + 2) 
 lholidayinfo(i).holidayname = lftv(i * 4 + 3) 
 next 
 
 '某月的第幾個(gè)星期幾 
 wftv = array( _ 
 5, 2, 1, "國際母親節(jié)", _ 
 5, 3, 1, "全國助殘日", _ 
 6, 3, 1, "父親節(jié)", _ 
 9, 3, 3, "國際和平日", _ 
 9, 4, 1, "國際聾人節(jié)", _ 
 10, 1, 2, "國際住房日", _ 
 10, 1, 4, "國際減輕自然災(zāi)害日", _ 
 11, 4, 5, "感恩節(jié)") 
 b = ubound(wftv) + 1 
 redim wholidayinfo(b / 4) 
 for i = 0 to (b / 4) - 1 
 wholidayinfo(i).month = wftv(i * 4) 
 wholidayinfo(i).weekatmonth = wftv(i * 4 + 1) 
 wholidayinfo(i).weekday = wftv(i * 4 + 2) '1 代表星期天 
 wholidayinfo(i).holidayname = wftv(i * 4 + 3) 
 next 
end sub 
'///////////////////////////////////////////////////////////////////////////////////////////////////////////// 
'計(jì)算農(nóng)歷上的節(jié)氣 
public property get lsolarterm() as string 
 '//===== 某年的第n個(gè)節(jié)氣為幾日(從0小寒起算) 
 'function sterm(y,n) { 
 ' var offdate = new date( ( 31556925974.7*(y-1900) + sterminfo[n]*60000 ) + date.utc(1900,0,6,2,5) ) 
 ' return(offdate.getutcdate()) 
 '//節(jié)氣 
 ' tmp1 = sterm(y, m * 2) - 1 
 
 dim basedateandtime as date 
 dim newdate as date 
 dim num as double 
 dim y as long 
 dim tempstr as string 
 
 basedateandtime = #1/6/1900 2:05:00 am# 
 y = mvarsyear 
 tempstr = "" 
 
 dim i as long 
 for i = 1 to 24 
 num = 525948.76 * (y - 1900) + sterminfo(i - 1) 
 newdate = dateadd("n", num, basedateandtime) '按分鐘計(jì)算,之所以不按秒鐘計(jì)算,是因?yàn)闀?huì)溢出 
 if abs(datediff("d", newdate, mvardate)) = 0 then 
 tempstr = solarterm(i - 1) 
 exit for 
 end if 
 next 
 
 lsolarterm = tempstr 
end property 
'計(jì)算按第幾周星期幾計(jì)算的節(jié)日 
public property get wholiday() as string 
 dim w as long 
 dim i as long 
 dim b as long 
 dim firstday as date 
 dim tempstr as string 
 
 b = ubound(wholidayinfo) 
 for i = 0 to b 
 if wholidayinfo(i).month = mvarsmonth then '當(dāng)月份相當(dāng)時(shí) 
 w = weekday(mvardate) 
 if wholidayinfo(i).weekday = w then '僅當(dāng)星期幾也相等時(shí) 
 firstday = mvarsmonth & "/" & 1 & "/" & mvarsyear '取當(dāng)月第一天 
 if (datediff("ww", firstday, mvardate) = wholidayinfo(i).weekatmonth) then 
 tempstr = wholidayinfo(i).holidayname 
 end if 
 end if 
 end if 
 next 
 
 
 wholiday = tempstr 
end property 
public property get lholiday() as string 
 dim i as long 
 dim b as long 
 dim tempstr as string 
 dim oy as long 
 dim odate as date 
 dim ndate as date 
 
 tempstr = "" 
 b = ubound(lholidayinfo) 
 if mvarlmonth = 12 and (mvarlday = 29 or mvarlday = 30) then 
 '保 
 oy = mvarlyear '保存農(nóng)歷年數(shù) 
 odate = mvardate 
 ndate = mvardate + 1 
 call sinitdate(year(ndate), month(ndate), day(ndate)) '計(jì)算第二天的屬性 
 if oy = mvarlyear - 1 then '如果農(nóng)歷年數(shù)增加了1 
 tempstr = "除夕" 
 call sinitdate(year(odate), month(odate), day(odate)) '恢復(fù)到今天原有數(shù)據(jù) 
 
 end if 
 else 
 for i = 0 to b 
 if (lholidayinfo(i).month = mvarlmonth) and _ 
 (lholidayinfo(i).day = mvarlday) then 
 tempstr = lholidayinfo(i).holidayname 
 exit for 
 end if 
 next 
 end if 
 lholiday = tempstr 
end property 
'求公歷節(jié)日 
public property get sholiday() as string 
 dim i as long 
 dim b as long 
 dim tempstr as string 
 
 tempstr = "" 
 b = ubound(sholidayinfo) 
 for i = 0 to b 
 if (sholidayinfo(i).month = mvarsmonth) and _ 
 (sholidayinfo(i).day = mvarsday) then 
 tempstr = sholidayinfo(i).holidayname 
 exit for 
 end if 
 next 
 sholiday = tempstr 
end property 
'是否是農(nóng)歷的閏月 
public property get isleap() as boolean 
 isleap = mvarisleap 
end property 
public property get lday() as long 
 lday = mvarlday 
end property 
public property get lmonth() as long 
 lmonth = mvarlmonth 
end property 
public property get lyear() as long 
 lyear = mvarlyear 
end property 
public property get sweekday() as long 
 sweekday = weekday(mvardate) 
end property 
public property get sday() as long 
 sday = mvarsday 
end property 
public property get smonth() as long 
 smonth = mvarsmonth 
end property 
public property get syear() as long 
 syear = mvarsyear 
end property 
'//////////////////////////////////////////////////////////////////////////////////////////////////////// 
public function istoday(y as long, m as long, d as long) as boolean 
 
 if (year(date) = y) and _ 
 (month(date) = m) and _ 
 (day(date) = d) then 
 istoday = true 
 else 
 istoday = false 
 end if 
 
end function 
'根據(jù)年份不同計(jì)算當(dāng)年屬于什么朝代 
public function era(y as long) as string 
 dim tempstr as string 
 
 if y < 1874 then 
 tempstr = "未知" 
 else 
 if y <= 1908 then 
 tempstr = "清朝光緒" 
 if y = 1874 then 
 tempstr = tempstr & "元年" 
 else 
 tempstr = tempstr & upnumber(cstr(y - 1874)) & "年" 
 end if 
 else 
 if y <= 1910 then 
 tempstr = "清朝宣統(tǒng)" 
 if y = 1909 then 
 tempstr = tempstr & "元年" 
 else 
 tempstr = tempstr & upnumber(cstr(y - 1909 + 1)) & "年" 
 end if 
 else 
 if y < 1949 then 
 tempstr = "中華民國" 
 if y = 1912 then 
 tempstr = tempstr & "元年" 
  else 
 tempstr = tempstr & upnumber(cstr(y - 1912 + 1)) & "年" 
 end if 
 else 
 tempstr = "中華人民共和國成立" 
 if y = 1949 then 
 tempstr = tempstr & "了" 
 else 
 select case y 
 case 2000 
 tempstr = "千禧年" 
 case else 
 tempstr = tempstr & upnumber(cstr(y - 1949)) & "周年" 
 end select 
 end if 
 end if 
 end if 
 end if 
 end if 
 
 era = tempstr 
end function 
' 傳入 num 傳回干支, 0=甲子 
public function ganzhi(num as long) as string 
 dim tempstr as string 
 dim i as long 
 i = (num - 1864) mod 60 '計(jì)算干支 
 tempstr = gan(i mod 10) & zhi(i mod 12) 
 ganzhi = tempstr 
end function 
'計(jì)算年的屬相字串 
public function yearattribute(y as long) as string 
 yearattribute = animals((y - 1900) mod 12) 
end function 
'將數(shù)字漢化 
public function upnumber(dxs as string) as string 
'檢測為空時(shí) 
if trim(dxs) = "" then 
 upnumber = "" 
 exit function 
end if 
 
dim sw as integer, szup as integer, tempstr as string, dxstr as string 
 sw = len(trim(dxs)) 
 
 dim i as integer 
 for i = 1 to sw 
 tempstr = right(trim(dxs), i) 
 tempstr = left(tempstr, 1) 
 tempstr = converts(tempstr) 
 select case i 
 case 1 
 if tempstr = "零" then 
 tempstr = "" 
 else 
 tempstr = tempstr + "" 
 end if 
 case 2 
 if tempstr = "零" then 
 tempstr = "零" 
 else 
 tempstr = tempstr + "十" 
 end if 
 case 3 
 if tempstr = "零" then 
 tempstr = "零" 
 else 
 tempstr = tempstr + "百" 
 end if 
 case 4 
 if tempstr = "零" then 
 tempstr = "零" 
 else 
 tempstr = tempstr + "千" 
 end if 
 case 5 
 if tempstr = "零" then 
 tempstr = "萬" 
 else 
 tempstr = tempstr + "萬" 
 end if 
 case 6 
 if tempstr = "零" then 
 tempstr = "零" 
 else 
 tempstr = tempstr + "十" 
 end if 
 case 7 
 if tempstr = "零" then 
 tempstr = "零" 
 else 
 tempstr = tempstr + "百" 
 end if 
 case 8 
 if tempstr = "零" then 
 tempstr = "零" 
 else 
 tempstr = tempstr + "千" 
 end if 
 case 9 
 if tempstr = "零" then 
 tempstr = "億" 
 else 
 tempstr = tempstr + "億" 
 end if 
 end select 
 dim tempa as string 
 tempa = left(trim(dxstr), 1) 
 if tempstr = "零" then 
 select case tempa 
 case "零" 
 dxstr = dxstr 
 case "萬" 
 dxstr = dxstr 
 case "億" 
 dxstr = dxstr 
 case else 
 dxstr = tempstr + dxstr 
 end select 
 else 
 dxstr = tempstr + dxstr 
 end if 
 next 
 
 upnumber = dxstr 
end function 
private function converts(numstr as string) as string 
 select case val(numstr) 
 case 0 
 converts = "零" 
 case 1 
 converts = "一" 
 case 2 
 converts = "二" 
 case 3 
 converts = "三" 
 case 4 
 converts = "四" 
 case 5 
 converts = "五" 
 case 6 
 converts = "六" 
 case 7 
 converts = "七" 
 case 8 
 converts = "八" 
 case 9 
 converts = "九" 
 end select 
end function 
'中文日期 
public function cdaystr(d as long) as string 
 dim s as string 
 select case d 
 case 0 
 s = "" 
 case 10 
 s = "初十" 
 case 20 
 s = "二十" 
 case 30 
 s = "三十" 
 case else 
 s = nstr2(d / 10) '整數(shù)除法 
 s = s & nstr1(d mod 10) 
 end select 
 cdaystr = s 
end function 
'計(jì)算星座歸屬 
public function constellation(m as long, d as long) as string 
 dim y as long 
 dim tempdate as date 
 dim constellname as string 
 y = 2000 
 tempdate = m & "/" & d & "/" & y 
 select case tempdate 
 case #3/21/2003# to #4/19/2000# 
 constellname = "白羊" 
 case #4/20/2000# to #5/20/2000# 
 constellname = "金牛" 
 case #5/21/2000# to #6/21/2000# 
 constellname = "雙子" 
 case #6/22/2000# to #7/22/2000# 
 constellname = "巨蟹" 
 case #7/23/2000# to #8/22/2000# 
 constellname = "獅子" 
 case #8/23/2000# to #9/22/2000# 
 constellname = "處女" 
 case #9/23/2000# to #10/23/2000# 
 constellname = "天秤" 
 case #10/24/2000# to #11/21/2000# 
 constellname = "天蝎" 
 case #11/22/2000# to #12/21/2000# 
 constellname = "射手" 
 case #12/22/2000# to #12/31/2000# 
 constellname = "摩蝎" 
 case #1/1/2000# to #1/19/2000# 
 constellname = "摩蝎" 
 case #1/20/2000# to #2/18/2000# 
 constellname = "水瓶" 
 case #2/19/2000# to #3/20/2000# 
 constellname = "雙魚" 
 case else 
 constellname = "" 
 end select 
 constellation = constellname 
end function 
'///////////////////////////////////////////////////////////////////////////////////////////////////////// 
'以下為類內(nèi)部使用的一些函數(shù) 
'傳回農(nóng)歷 y年的總天數(shù) 
private function lyeardays(byval y as long) as long 
 
' dim i as long 
' dim f as long 
' dim sumday as long 
' dim info as long 
 
' sumday = 348 
' i = &h8000 
' info = lunarinfo(y - 1900) and &h1000ffff '屏蔽高位, 
' do 
' f = info and i 
' if f <> 0 then 
' sumday = sumday + 1 
' end if 
' i = bitright16(i, 1) 
' loop until i < &h10 
' lyeardays = sumday + leapdays(y) 
 
 lyeardays = lunaryeardays(y - 1900) '先計(jì)算出每年的天數(shù),并形成數(shù)組,以減少以后的運(yùn)算時(shí)間 
end function 
'傳回農(nóng)歷 y年m月的總天數(shù) 
private function lmonthdays(byval y as long, byval m as long) as long 
 if (lunarinfo(y - 1900) and &h1000ffff) and bitright32(&h10000, m) then 
 lmonthdays = 30 
 else 
 lmonthdays = 29 
 end if 
end function 
'傳回農(nóng)歷 y年閏月的天數(shù) 
private function leapdays(y as long) as long 
 if leapmonth(y) then 
 if lunarinfo(y - 1900) and &h10000 then 
 leapdays = 30 
 else 
 leapdays = 29 
 end if 
 else 
 leapdays = 0 
 end if 
end function 
'傳回農(nóng)歷 y年閏哪個(gè)月 1-12 , 沒閏傳回 0 
private function leapmonth(y as long) as long 
 dim i as long 
 i = lunarinfo(y - 1900) and &hf 
 if i > 12 then 
 debug.print y 
 end if 
 leapmonth = i 
end function 
'計(jì)算公歷年月的天數(shù) 
private function solardays(y as long, m as long) as long 
 dim d as long 
 
 if (y mod 4) = 0 then '閏年 
 if m = 2 then 
 d = 29 
 else 
 d = solarmonth(m - 1) 
 end if 
 else 
 if m = 2 then 
 d = 28 
 else 
 d = solarmonth(m - 1) 
 end if 
 end if 
 
 solardays = d 
end function 
'////////////////////////////////////////////////////////////////////////////////////////////////// 
' 
'主要的函數(shù),用公歷年月日對日期對象進(jìn)行初使化,在此函數(shù)內(nèi)部完成對私有對象屬性的設(shè)置 
' 
'////////////////////////////////////////////////////////////////////////////////////////////////// 
public sub sinitdate(byval y as long, byval m as long, byval d as long) 
 dim i as long 
 dim leap as long 
 dim temp as long 
 dim offset as long 
 
 mvardate = m & "/" & d & "/" & y 
 mvarsyear = y 
 mvarsmonth = m 
 mvarsday = d 
 
 '農(nóng)歷日期計(jì)算部分 
 leap = 0 
 temp = 0 
 
 offset = mvardate - #1/30/1900# '計(jì)算兩天的基本差距 
 
 for i = 1900 to 2049 
 'temp = lyeardays(i) '求當(dāng)年農(nóng)歷年天數(shù) 
 
 offset = offset - temp 
 if offset < 1 then exit for 
 next 
 
 offset = offset + temp 
 mvarlyear = i 
 
 leap = leapmonth(i) '閏哪個(gè)月 
 mvarisleap = false 
 for i = 1 to 12 
 '閏月 
 if leap > 0 and i = (leap + 1) and mvarisleap = false then 
 mvarisleap = true 
 i = i - 1 
 temp = leapdays(mvarlyear) '計(jì)算閏月天數(shù) 
 else 
 temp = lmonthdays(mvarlyear, i) '計(jì)算非閏月天數(shù) 
 end if 
 
 offset = offset - temp 
 if offset <= 0 then exit for 
 next 
 
 offset = offset + temp 
 mvarlmonth = i 
 mvarlday = offset 
 
end sub 
'////////////////////////////////////////////////////////////////////////////////////////////////// 
' 
'主要的函數(shù),用農(nóng)歷年月日對日期對象進(jìn)行初使化,在此函數(shù)內(nèi)部完成對私有對象屬性的設(shè)置 
' 
'////////////////////////////////////////////////////////////////////////////////////////////////// 
public sub linitdate(byval y as long, byval m as long, byval d as long, optional leapflag as boolean = false) 
 dim i as long 
 dim leap as long 
 dim temp as long 
 dim offset as long 
 mvarlyear = y 
 mvarlmonth = m 
 mvarlday = d 
 
 offset = 0 
 
 for i = 1900 to y - 1 
 temp = lunaryeardays(i - 1900) '求當(dāng)年農(nóng)歷年天數(shù) 
 offset = offset + temp 
 next 
 
 leap = leapmonth(y) '閏哪個(gè)月 
 if m <> leap then 
 mvarisleap = false '當(dāng)前日期并非閏月 
 else 
 mvarisleap = leapflag '使用用戶輸入的是否閏月月份 
 end if 
 
 if (m < leap) or (leap = 0) then '當(dāng)閏月在當(dāng)前日期后 
 for i = 1 to m - 1 
 temp = lmonthdays(y, i) '計(jì)算非閏月天數(shù) 
 offset = offset + temp 
 next 
 else '在閏月后 
 if mvarisleap = false then '用戶要計(jì)算非閏月的月份 
 for i = 1 to m - 1 
 temp = lmonthdays(y, i) '計(jì)算非閏月天數(shù) 
 offset = offset + temp 
 next 
 if m > leap then 
 temp = leapdays(y) '計(jì)算閏月天數(shù) 
 offset = offset + temp 
 end if 
 
 else '此時(shí)只有mvarisleap=ture, 
 for i = 1 to m 
 temp = lmonthdays(y, i) '計(jì)算非閏月天數(shù) 
 offset = offset + temp 
 next 
 end if 
 end if 
 
 offset = offset + d '加上當(dāng)月的天數(shù) 
 mvardate = dateadd("d", offset, #1/30/1900#) 
 mvarsyear = year(mvardate) 
 mvarsmonth = month(mvardate) 
 mvarsday = day(mvardate) 
end sub 
'本模塊用于打印出1900-2049年 每年農(nóng)歷的天數(shù),可以用于數(shù)組初使化 
'public sub printf() 
' dim i as long, j as long 
' dim temp(10) as long 
' dim base as long 
 
' base = 1900 
' for i = 1 to 15 
' for j = 1 to 10 
' temp(j - 1) = lyeardays((i - 1) * 10 + (j - 1) + base) '求當(dāng)年農(nóng)歷年天數(shù) 
' next 
' debug.print cstr(temp(0)) & " , " & cstr(temp(1)) & " , " & cstr(temp(2)) & " , " & cstr(temp(3)) & " , " & cstr(temp(4)) & " , " & cstr(temp(5)) & " , " & cstr(temp(6)) & " , " & cstr(temp(7)) & " , " & cstr(temp(8)) & " , " & cstr(temp(9)) & " , " & " _ " 
' next 
'end sub