隐藏

asp用类实现公历与农历相互转换

发布:2014/5/10 15:45:57作者:管理员 来源:本站 浏览次数:1332

asp用类实现公历与农历相互转换

<%
'##########################################################
'Class NongLiGongLi 用于农历与公历间的相互转换
'本类可处理1901-2050年(农历)之间的公农历转换
'应用示例:
' Dim NongGong
' Set NongGong = New NongLiGongLi

'公历转农历(查询日期范围1901-2-19至2051-2-10,格式yyyy-mm-dd)
' Response.Write NongGong.GongToNong("1984-12-10")

'农历转公历(查询日期范围1901-1-1至2050-12-29,格式yyyy-mm-dd,Ntype为"闰"或"1",表示查询农历的月份是闰月;Ntype为""或其他值,表示不是闰月)
' Response.Write NongGong.NongToGong("1984-10-18",Ntype)
'##########################################################

Class NongLiGongLi

     Dim GongLi(12),NongLiStart,NongLi(149)

     Sub Class_Initialize()
     '定义公历月份天数
     GongLi(1)=31
     GongLi(2)=28
     GongLi(3)=31
     GongLi(4)=30
     GongLi(5)=31
     GongLi(6)=30
     GongLi(7)=31
     GongLi(8)=31
     GongLi(9)=30
     GongLi(10)=31
     GongLi(11)=30
     GongLi(12)=31

     '定义农历数据
     NongLiStart=1901     '农历从1901年开始
     '差:该年的农历正月初一到该年公历1月1日的天数;1~12:农历月份天数;闰:如有闰月,记录该月平月天数
             '   差   1   2   3   4   5   6   7   8   9 10 11 12 闰
  NongLi(0)="49,29,30,29,29,30,29,30,29,30,30,30,29"
  NongLi(1)="38,30,29,30,29,29,30,29,30,29,30,30,30"
  NongLi(2)="28,29,30,29,30,58,30,29,29,30,30,29,30,29" '五月29 闰五月29
  NongLi(3)="46,30,30,29,30,29,29,30,29,29,30,30,29"
  NongLi(4)="34,30,30,29,30,30,29,29,30,29,30,29,30"
  NongLi(5)="24,29,30,30,59,29,30,29,30,29,30,29,30,29"'四月29 闰四月30
  NongLi(6)="43,29,30,29,30,29,30,30,29,30,29,30,29"
  NongLi(7)="32,30,29,29,30,30,29,30,29,30,30,29,30"
  NongLi(8)="21,29,59,29,30,29,30,29,30,30,30,29,30,30"
  NongLi(9)="40,29,30,29,29,30,29,30,29,30,30,30,29"
  NongLi(10)="29,30,29,30,29,29,59,29,30,30,29,30,30,30"
  NongLi(11)="48,30,29,30,29,29,30,29,29,30,30,29,30"
  NongLi(12)="36,30,30,29,30,29,29,30,29,29,30,29,30"
  NongLi(13)="25,30,30,29,30,59,29,30,29,29,30,29,30,29"
  NongLi(14)="44,30,29,30,30,29,30,29,30,29,30,29,29"
  NongLi(15)="33,30,30,29,30,29,30,30,29,30,29,30,29"
  NongLi(16)="22,30,58,30,29,30,30,29,30,30,29,30,29,29"
  NongLi(17)="41,30,29,29,30,29,30,29,30,30,29,30,30"
  NongLi(18)="31,29,30,29,29,30,29,59,30,29,30,30,30,29"
  NongLi(19)="50,29,30,29,29,30,29,29,30,29,30,30,30"
  NongLi(20)="38,30,29,30,29,29,30,29,29,30,29,30,30"
  NongLi(21)="27,30,29,30,30,58,30,29,29,30,29,30,30,29"
  NongLi(22)="46,29,30,30,29,30,29,30,29,29,30,29,30"
  NongLi(23)="35,29,30,30,29,30,30,29,30,29,30,29,29"
  NongLi(24)="23,30,29,30,59,30,29,30,30,29,30,29,30,29"
  NongLi(25)="43,29,29,30,29,30,29,30,30,29,30,30,29"
  NongLi(26)="32,30,29,29,30,29,30,29,30,29,30,30,30"
  NongLi(27)="22,29,59,29,30,29,29,30,29,30,30,30,30,30"
  NongLi(28)="40,29,30,29,29,30,29,29,30,29,30,30,30"
  NongLi(29)="29,29,30,30,29,29,59,29,30,29,30,30,29,30"
  NongLi(30)="47,30,30,29,30,29,30,29,29,30,29,30,29"
  NongLi(31)="36,30,30,30,29,30,29,30,29,29,30,29,30"
  NongLi(32)="25,29,30,30,29,59,30,30,29,30,29,29,30,30"
  NongLi(33)="44,29,30,29,30,30,29,30,29,30,30,29,30"
  NongLi(34)="34,29,29,30,29,30,29,30,30,29,30,30,29"
  NongLi(35)="23,30,29,59,29,29,30,30,29,30,30,30,29,29"
  NongLi(36)="41,30,29,29,30,29,29,30,29,30,30,30,29"
  NongLi(37)="30,30,30,29,29,30,29,59,29,30,30,29,30,29"
  NongLi(38)="49,30,30,29,29,30,29,29,30,29,30,29,30"
  NongLi(39)="38,30,30,29,30,29,30,29,29,30,29,30,29"
  NongLi(40)="26,30,30,29,30,30,59,29,29,30,29,30,29,29"
  NongLi(41)="45,30,29,30,30,29,30,29,30,29,30,29,30"
  NongLi(42)="35,29,30,29,30,29,30,30,29,30,29,30,29"
  NongLi(43)="24,30,29,30,59,29,30,29,30,30,29,30,30,29"
  NongLi(44)="43,29,29,30,29,29,30,29,30,30,30,29,30"
  NongLi(45)="32,30,29,29,30,29,29,30,29,30,30,29,30"
  NongLi(46)="21,30,59,29,30,29,29,30,29,30,29,30,30,30"
  NongLi(47)="40,30,29,30,29,30,29,29,30,29,30,29,30"
  NongLi(48)="28,30,29,30,30,29,30,58,30,29,30,29,30,29"
  NongLi(49)="47,29,30,30,29,30,30,29,29,30,29,30,29"
  NongLi(50)="36,30,29,30,30,29,30,29,30,29,30,29,30"
  NongLi(51)="26,29,30,29,30,59,29,30,30,29,30,29,30,29"
  NongLi(52)="44,29,30,29,29,30,30,29,30,30,29,30,29"
  NongLi(53)="33,30,29,30,29,29,30,29,30,30,29,30,30"
  NongLi(54)="23,29,30,59,29,29,30,29,30,29,30,30,30,29"
  NongLi(55)="42,29,30,29,30,29,29,30,29,30,29,30,30"
  NongLi(56)="30,30,29,30,29,30,29,29,59,30,29,30,29,30"
  NongLi(57)="48,30,30,30,29,30,29,29,30,29,30,29,30"
  NongLi(58)="38,29,30,30,29,30,29,30,29,30,29,30,29"
  NongLi(59)="27,30,29,30,29,30,59,30,29,30,29,30,29,30"
  NongLi(60)="45,30,29,30,29,30,29,30,30,29,30,29,30"
  NongLi(61)="35,29,30,29,29,30,29,30,30,29,30,30,29"
  NongLi(62)="24,30,29,30,58,30,29,30,29,30,30,30,29,29"
  NongLi(63)="43,30,29,30,29,29,30,29,30,29,30,30,30"
  NongLi(64)="32,29,30,29,30,29,29,30,29,29,30,30,29"
  NongLi(65)="20,30,30,59,30,29,29,30,29,29,30,30,29,30"
  NongLi(66)="39,30,30,29,30,30,29,29,30,29,30,29,30"
  NongLi(67)="29,29,30,29,30,30,29,59,30,29,30,29,30,30"
  NongLi(68)="47,29,30,29,30,29,30,30,29,30,29,30,29"
  NongLi(69)="36,30,29,29,30,29,30,30,29,30,30,29,30"
  NongLi(70)="26,29,30,29,29,59,30,29,30,30,30,29,30,30"
  NongLi(71)="45,29,30,29,29,30,29,30,29,30,30,29,30"
  NongLi(72)="33,30,29,30,29,29,30,29,29,30,30,29,30"
  NongLi(73)="22,30,30,29,59,29,30,29,29,30,30,29,30,30"
  NongLi(74)="41,30,30,29,30,29,29,30,29,29,30,29,30"
  NongLi(75)="30,30,30,29,30,29,30,29,59,29,30,29,30,30"
  NongLi(76)="48,30,29,30,30,29,30,29,30,29,30,29,29"
  NongLi(77)="37,30,29,30,30,29,30,30,29,30,29,30,29"
  NongLi(78)="27,30,29,29,30,29,60,29,30,30,29,30,29,30"
  NongLi(79)="46,30,29,29,30,29,30,29,30,30,29,30,30"
  NongLi(80)="35,29,30,29,29,30,29,29,30,30,29,30,30"
  NongLi(81)="24,30,29,30,58,30,29,29,30,29,30,30,30,29"
  NongLi(82)="43,30,29,30,29,29,30,29,29,30,29,30,30"
  NongLi(83)="32,30,29,30,30,29,29,30,29,29,59,30,30,30"
  NongLi(84)="50,29,30,30,29,30,29,30,29,29,30,29,30"
  NongLi(85)="39,29,30,30,29,30,30,29,30,29,30,29,29"
  NongLi(86)="28,30,29,30,29,30,59,30,30,29,30,29,29,30"
  NongLi(87)="47,30,29,30,29,30,29,30,30,29,30,30,29"
  NongLi(88)="36,30,29,29,30,29,30,29,30,29,30,30,30"
  NongLi(89)="26,29,30,29,29,59,29,30,29,30,30,30,30,30"
  NongLi(90)="45,29,30,29,29,30,29,29,30,29,30,30,30"
  NongLi(91)="34,29,30,30,29,29,30,29,29,30,29,30,30"
  NongLi(92)="22,29,30,59,30,29,30,29,29,30,29,30,29,30"
  NongLi(93)="40,30,30,30,29,30,29,30,29,29,30,29,30"
  NongLi(94)="30,29,30,30,29,30,29,30,59,29,30,29,30,30"
  NongLi(95)="49,29,30,29,30,30,29,30,29,30,30,29,29"
  NongLi(96)="37,30,29,30,29,30,29,30,30,29,30,30,29"
  NongLi(97)="27,30,29,29,30,58,30,30,29,30,30,29,30,29"
  NongLi(98)="46,30,29,29,30,29,29,30,29,30,30,30,29"
  NongLi(99)="35,30,30,29,29,30,29,29,30,29,30,30,29"
  NongLi(100)="23,30,30,29,59,30,29,29,30,29,30,29,30,30"
  NongLi(101)="42,30,30,29,30,29,30,29,29,30,29,30,29"
  NongLi(102)="31,30,30,29,30,30,29,30,29,29,30,29,30"
  NongLi(103)="21,29,59,30,30,29,30,29,30,29,30,29,30,30"
  NongLi(104)="39,29,30,29,30,29,30,30,29,30,29,30,29"
  NongLi(105)="28,30,29,30,29,30,29,59,30,30,29,30,30,30"
  NongLi(106)="48,29,29,30,29,29,30,29,30,30,30,29,30"
  NongLi(107)="37,30,29,29,30,29,29,30,29,30,30,29,30"
  NongLi(108)="25,30,30,29,29,59,29,30,29,30,29,30,30,30"
  NongLi(109)="44,30,29,30,29,30,29,29,30,29,30,29,30"
  NongLi(110)="33,30,29,30,30,29,30,29,29,30,29,30,29"
  NongLi(111)="22,30,29,30,59,30,29,30,29,30,29,30,29,30"
  NongLi(112)="40,30,29,30,29,30,30,29,30,29,30,29,30"
  NongLi(113)="30,29,30,29,30,29,30,29,30,59,30,29,30,30"
  NongLi(114)="49,29,30,29,29,30,29,30,30,30,29,30,29"
  NongLi(115)="38,30,29,30,29,29,30,29,30,30,29,30,30"
  NongLi(116)="27,29,30,29,30,29,59,29,30,29,30,30,30,29"
  NongLi(117)="46,29,30,29,30,29,29,30,29,30,29,30,30"
  NongLi(118)="35,30,29,30,29,30,29,29,30,29,29,30,30"
  NongLi(119)="24,29,30,30,59,30,29,29,30,29,30,29,30,30"
  NongLi(120)="42,29,30,30,29,30,29,30,29,30,29,30,29"
  NongLi(121)="31,30,29,30,29,30,30,29,30,29,30,29,30"
  NongLi(122)="21,29,59,29,30,30,29,30,30,29,30,29,30,30"
  NongLi(123)="40,29,30,29,29,30,29,30,30,29,30,30,29"
  NongLi(124)="28,30,29,30,29,29,59,30,29,30,30,30,29,30"
  NongLi(125)="47,30,29,30,29,29,30,29,29,30,30,30,29"
  NongLi(126)="36,30,30,29,30,29,29,30,29,29,30,30,29"
  NongLi(127)="25,30,30,30,29,59,29,30,29,29,30,30,29,30"
  NongLi(128)="43,30,30,29,30,29,30,29,30,29,29,30,30"
  NongLi(129)="33,29,30,29,30,30,29,30,29,30,29,30,29"
  NongLi(130)="22,29,30,59,30,29,30,30,29,30,29,30,29,30"
  NongLi(131)="41,30,29,29,30,29,30,30,29,30,30,29,30"
  NongLi(132)="30,29,30,29,29,30,29,30,29,30,30,59,30,30"
  NongLi(133)="49,29,30,29,29,30,29,30,29,30,30,29,30"
  NongLi(134)="38,30,29,30,29,29,30,29,29,30,30,29,30"
  NongLi(135)="27,30,30,29,30,29,59,29,29,30,29,30,30,29"
  NongLi(136)="45,30,30,29,30,29,29,30,29,29,30,29,30"
  NongLi(137)="34,30,30,29,30,29,30,29,30,29,29,30,29"
  NongLi(138)="23,30,30,29,30,59,30,29,30,29,30,29,29,30"
  NongLi(139)="42,30,29,30,30,29,30,29,30,30,29,30,29"
  NongLi(140)="31,29,30,29,30,29,30,30,29,30,30,29,30"
  NongLi(141)="21,29,59,29,30,29,30,29,30,30,29,30,30,30"
  NongLi(142)="40,29,30,29,29,30,29,29,30,30,29,30,30"
  NongLi(143)="29,30,29,30,29,29,30,58,30,29,30,30,30,29"
  NongLi(144)="47,30,29,30,29,29,30,29,29,30,29,30,30"
  NongLi(145)="36,30,29,30,29,30,29,30,29,29,30,29,30"
  NongLi(146)="25,30,29,30,30,59,29,30,29,29,30,29,30,29"
  NongLi(147)="44,29,30,30,29,30,30,29,30,29,29,30,29"
  NongLi(148)="32,30,29,30,29,30,30,29,30,30,29,30,29"
  NongLi(149)="22,29,30,59,29,30,29,30,30,29,30,30,29,29"
     End Sub
  
  '根据年月获取农历天数
  Function getNongliDayNumByMonth(y,m)
  str = NongLi(birthYear-NongLiStart)
  arr= split(str,",")
  days=int(arr(m))
  if days>30 then
   days=arr(13)
  end if
  getNongliDayNumByMonth=days
  End Function
  
  '农历增加天数
  'flag表示是否为闰月
  Function dayAddNongli(dateStr,num,flag)
   yearStr=year(dateStr)
  monthStr=month(dateStr)
  dayStr=day(dateStr)
  str = NongLi(yearStr-NongLiStart)
  arr= split(str,",")
  days=int(arr(monthStr))
  if days>30 then
   if flag=1 then
    days=days-arr(13)
   else
    days=arr(13)
   end if
  end if
  
  pday=dayStr+num
  sday=0
  tag=false
  if pday>days then
   for i=monthStr to 12
    sday=sday+int(arr(i))
    if sday>=pday then
     monthStr=i
     tag=true
     exit for
    end if
   next
   if tag=false then
    yearStr=yearStr+1
    str = NongLi(yearStr-NongLiStart)
    arr= split(str,",")
    for i=1 to 12
     sday=sday+int(arr(i))
     if sday>=pday then
      monthStr=i
      tag=true
      exit for
     end if
    next
   end if
   if tag then
    pday=int(arr(monthStr))-(sday-pday)
    dayAddNongli=yearStr&"-"&monthStr&"-"&pday
   else
    dayAddNongli=dateadd("d",num,date())
   end if
  else
   dayAddNongli=yearStr&"-"&monthStr&"-"&pday
  end if
  
  End function
    
     '公历该月的天数(y:年份; m:月份)
     Function GongliMonth(y,m)
         If m=2 And ((y Mod 400 =0) or (y Mod 4 =0 And y Mod 100 <> 0)) Then
             GongliMonth=29
         Else
             GongliMonth=GongLi(m)
         End If
     End Function

     '农历月份名称转换(m:月份)
     Function NongliMonth(m)
         If m>=1 And m<=12 Then
             MonthStr=",正,二,三,四,五,六,七,八,九,十,十一,十二"
             MonthStr=Split(MonthStr,",")
             NongliMonth=MonthStr(m)
         Else
             NongliMonth=m
         End If
     End Function

     '农历月份名称转换(d:日)
     Function NongliDay(d)
         If d>=1 And d<=30 Then
             DayStr=",初一,初二,初三,初四,初五,初六,初七,初八,初九,初十,十一,十二,十三,十四,十五,十六,十七,十八,十九,二十,廿一,廿二,廿三,廿四,廿五,廿六,廿七,廿八,廿九,三十"
             DayStr=Split(DayStr,",")
             NongliDay=DayStr(d)
         Else
             NongliDay=d
         End If
     End Function

     '公历转农历(Gdate:公历日期)
     Function GongToNong(Gdate)
         If IsDate(Gdate)=False Then
             response.write "<script language=javascript>alert('出错!非日期类型,错误会输出1900-1-1')</script>"
             GongToNong="1900-1-1"
             Exit Function
         End If

         If CDate(Gdate) < #1901-1-1# or CDate(Gdate) > #2051-2-10# Then
             response.write "<script language=javascript>alert('出错!目前公历只支持1901-1-1至2051-2-10,错误会输出1900-1-1')</script>"
             GongToNong="1900-1-1"
             Exit Function
         End If


         Dim Gyear,Gmonth,Gday,Glen,Narr,Nyear,Nmonth,Nday,Ni,Ntype

         Gyear=Year(Gdate)
         Gmonth=Month(Gdate)
         Gday=Day(Gdate)

         Glen=DateDiff("d",Gyear &"-1-1",Gdate)+1     '获取查询日期到当年1月1日的天数
         Narr=Split(NongLi(Gyear-NongLiStart),",")     '获取相应年度农历数据,化成数组Narr
         If Glen<=CInt(Narr(0)) Then
             Nyear=Gyear-1
             Glen=CInt(Narr(0))-Glen
             Narr=Split(NongLi(Nyear-NongLiStart),",")
             If Glen<CInt(Narr(12)) Then
                 Nmonth=12
                 Nday=CInt(Narr(12))-Glen
             Else
                 Nmonth=11
                 Glen=Glen-CInt(Narr(12))
                 Nday=CInt(Narr(11))-Glen
             End If
         Else
             Nyear=Gyear
             Glen=Glen-CInt(Narr(0))
             For Ni=1 To 12
                 If Glen>CInt(Narr(Ni)) Then
                     Glen=Glen-CInt(Narr(Ni))
                 Else
                     If Glen>30 Then
                         Glen=Glen-CInt(Narr(13))
                         Ntype="闰"     '闰月
                     End If
                     Nmonth=Ni
                     Nday=Glen
                     Exit For
                 End If
             Next
         End If

'         GongToNong="农历"& Nyear &"年"& Ntype & NongliMonth(Nmonth) &"月"& NongliDay(Nday)     '效果:农历2000年(闰)四月初六
'         GongToNong="农历"& Nyear &"年"& Ntype & Nmonth &"月"& Nday &"日"     '效果:农历2000年(闰)4月6日
         GongToNong=Nyear &"-"& Nmonth &"-"& Nday &" "& Ntype         '效果:2000-4-6 (闰)
     End Function

     '农历转公历(Ndate:农历日期; Ntype:是否闰月)
     Function NongToGong(Ndate,Ntype)
         If IsDate(Ndate)=False And Right(Ndate,4)<>"2-29" And Right(Ndate,4)<>"2-30" Then
             response.write "<script language=javascript>alert('出错!非日期类型,错误会输出1900-1-1')</script>"
             NongToGong="1900-1-1"
             Exit Function
         End If
         If CInt(Left(Ndate,4)) < NongLiStart or Left(Ndate,4) > 2050 Then
             response.write "<script language=javascript>alert('出错!目前农历只支持1950-1-1至2050-12-29,错误会输出1900-1-1')</script>"
             NongToGong="1900-1-1"
             Exit Function
         End If

         Dim Nyear,Nmonth,Nday,Narr,Nlen,Ni,Gyear,Gmonth,Gday,Gi

'         Nyear=Year(Ndate)
'         Nmonth=Month(Ndate)
'         Nday=Day(Ndate)
         '因为农历日期存在2月29或30,故人工截取年、月、日
         Nyear=Split(Ndate,"-")(0)
         Nmonth=Split(Ndate,"-")(1)
         Nday=Split(Ndate,"-")(2)
         If Ntype="闰" or Ntype="1" Then Ntype="闰" Else Ntype=""         '判断查询日期是否是闰月

         Narr=Split(NongLi(Nyear-NongLiStart),",")         '获取相应年度农历数据,化成数组Narr
         If Ntype="闰" And UBound(Narr)<=12 Then
             response.write "<script language=javascript>alert('农历"& Ndate &"无闰月,将按照平月计算')</script>"
         End If

         '如果查询的农历是闰月并该年度农历数组存在闰月数据就获取
         If Narr(Nmonth)>30 And Ntype="闰" And UBound(Narr)>=13 Then
             Nday=CInt(Narr(13))+Nday
         End If

         '获取该年农历日期到公历1月1日的天数
         Nlen=Nday
         For Ni=0 To Nmonth-1
             Nlen=Nlen+CInt(Narr(Ni))
         Next

         If Nlen>366 or (GongliMonth(Nyear,2)<>29 And Nlen>365) Then
         '当查询农历日期距离公历1月1日超过一年时
             Gyear=Nyear+1
             If GongliMonth(Nyear,2)<>29 Then Nlen=Nlen-365 Else Nlen=Nlen-366
             If Nlen>GongLi(1) Then
                 Gmonth=2
                 Gday=Nlen-GongLi(1)
             Else
                 Gmonth=1
                 Gday=Nlen
             End If
         Else
             Gyear=Nyear
             For Gi=1 To 12
                 If Nlen>GongliMonth(Gyear,Gi) Then
                     Nlen=Nlen-GongliMonth(Gyear,Gi)
                 Else
                     Gmonth=Gi
                     Gday=Nlen
                     Exit For
                 End If
             Next
         End If

         NongToGong=Gyear &"-"& Gmonth &"-"& Gday
     End Function

End Class
%>