设为首页收藏本站

SKY外语、计算机论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: fieldmax
打印 上一主题 下一主题

VB_计算农历的算法

[复制链接]

16

主题

0

好友

216

积分

中级会员

Rank: 3Rank: 3

生肖
性别

最佳新人 论坛元老

跳转到指定楼层
楼主
发表于 2012-4-28 21:42:25 |只看该作者 |倒序浏览
本帖最后由 fieldmax 于 2012-4-28 22:36 编辑

VB计算农历的算法
作者:  评价:  上站日期: 2002-05-22  
内容说明:  
来源:  

--------------------------------------------------------------------------------


'下面是一个关于VB的农历算法
'日期数据定义方法如下
'前12个字节代表1-12月为大月或是小月,1为大月30天,0为小月29天,
'第13位为闰月的情况,1为大月30天,0为小月29天,第14位为闰月的月
'份,如果不是闰月为0,否则给出月份,10、11、12分别用A、B、C来表
'示,即使用16进制。最后4位为当年家农历新年-即农历1月1日所在公历
'的日期,如0131代表1月31日。
'GetYLDate函数使用方式如下tYear为要输入的年,tMonth为月,tDay为
'日期,YLyear是返回值,返加农历的年份,如甲子年,YLShuXing返回
'的是属象,如鼠。IsGetGl是设置是不是通过农历取公历值,如果是,
'前三个返回相应的公历日期,而且返回值是一个公历日期。

  1. Function GetYLDate(tYear As Integer, tMonth As Integer, tDay As Integer, _
  2.                     YLyear As String, YLShuXing As String, _
  3.                     Optional IsGetGl As Boolean) As String

  4.     On Error Resume Next
  5.     Dim daList(1900 To 2011) As String * 18
  6.     Dim conDate As Date, setDate As Date
  7.     Dim AddMonth As Integer, AddDay As Integer, AddYear As Integer, getDay As Integer
  8.     Dim RunYue As Boolean
  9.     If tYear >  2010 Or tYear <  1901 Then Exit Function '如果不是有效有日期,退出
  10.     '1900 to 1909
  11.     daList(1900) = "010010110110180131"
  12.     daList(1901) = "010010101110000219"
  13.     daList(1902) = "101001010111000208"
  14.     daList(1903) = "010100100110150129"
  15.     daList(1904) = "110100100110000216"
  16.     daList(1905) = "110110010101000204"
  17.     daList(1906) = "011010101010140125"
  18.     daList(1907) = "010101101010000213"
  19.     daList(1908) = "100110101101000202"
  20.     daList(1909) = "010010101110120122"
  21.     daList(1910) = "010010101110000210"
  22.     daList(1911) = "101001001101160130"
  23.     daList(1912) = "101001001101000218"
  24.     daList(1913) = "110100100101000206"
  25.     daList(1914) = "110101010100150126"
  26.     daList(1915) = "101101010101000214"
  27.     daList(1916) = "010101101010000204"
  28.     daList(1917) = "100101101101020123"
  29.     daList(1918) = "100101011011000211"
  30.     daList(1919) = "010010011011170201"
  31.     daList(1920) = "010010011011000220"
  32.     daList(1921) = "101001001011000208"
  33.     daList(1922) = "101100100101150128"
  34.     daList(1923) = "011010100101000216"
  35.     daList(1924) = "011011010100000205"
  36.     daList(1925) = "101011011010140124"
  37.     daList(1926) = "001010110110000213"
  38.     daList(1927) = "100101010111000202"
  39.     daList(1928) = "010010010111120123"
  40.     daList(1929) = "010010010111000210"
  41.     daList(1930) = "011001001011060130"
  42.     daList(1931) = "110101001010000217"
  43.     daList(1932) = "111010100101000206"
  44.     daList(1933) = "011011010100150126"
  45.     daList(1934) = "010110101101000214"
  46.     daList(1935) = "001010110110000204"
  47.     daList(1936) = "100100110111030124"
  48.     daList(1937) = "100100101110000211"
  49.     daList(1938) = "110010010110170131"
  50.     daList(1939) = "110010010101000219"
  51.     daList(1940) = "110101001010000208"
  52.     daList(1941) = "110110100101060127"
  53.     daList(1942) = "101101010101000215"
  54.     daList(1943) = "010101101010000205"
  55.     daList(1944) = "101010101101140125"
  56.     daList(1945) = "001001011101000213"
  57.     daList(1946) = "100100101101000202"
  58.     daList(1947) = "110010010101120122"
  59.     daList(1948) = "101010010101000210"
  60.     daList(1949) = "101101001010170129"
  61.     daList(1950) = "011011001010000217"
  62.     daList(1951) = "101101010101000206"
  63.     daList(1952) = "010101011010150127"
  64.     daList(1953) = "010011011010000214"
  65.     daList(1954) = "101001011011000203"
  66.     daList(1955) = "010100101011130124"
  67.     daList(1956) = "010100101011000212"
  68.     daList(1957) = "101010010101080131"
  69.     daList(1958) = "111010010101000218"
  70.     daList(1959) = "011010101010000208"
  71.     daList(1960) = "101011010101060128"
  72.     daList(1961) = "101010110101000215"
  73.     daList(1962) = "010010110110000205"
  74.     daList(1963) = "101001010111040125"
  75.     daList(1964) = "101001010111000213"
  76.     daList(1965) = "010100100110000202"
  77.     daList(1966) = "111010010011030121"
  78.     daList(1967) = "110110010101000209"
  79.     daList(1968) = "010110101010170130"
  80.     daList(1969) = "010101101010000217"
  81.     daList(1970) = "100101101101000206"
  82.     daList(1971) = "010010101110150127"
  83.     daList(1972) = "010010101101000215"
  84.     daList(1973) = "101001001101000203"
  85.     daList(1974) = "110100100110140123"
  86.     daList(1975) = "110100100101000211"
  87.     daList(1976) = "110101010010180131"
  88.     daList(1977) = "101101010100000218"
  89.     daList(1978) = "101101101010000207"
  90.     daList(1979) = "100101101101060128"
  91.     daList(1980) = "100101011011000216"
  92.     daList(1981) = "010010011011000205"
  93.     daList(1982) = "101001001011140125"
  94.     daList(1983) = "101001001011000213"
  95.     daList(1984) = "1011001001011A0202"
  96.     daList(1985) = "011010100101000220"
  97.     daList(1986) = "011011010100000209"
  98.     daList(1987) = "101011011010060129"
  99.     daList(1988) = "101010110110000217"
  100.     daList(1989) = "100100110111000206"
  101.     daList(1990) = "010010010111150127"
  102.     daList(1991) = "010010010111000215"
  103.     daList(1992) = "011001001011000204"
  104.     daList(1993) = "011010100101030123"
  105.     daList(1994) = "111010100101000210"
  106.     daList(1995) = "011010110010180131"
  107.     daList(1996) = "010110101100000219"
  108.     daList(1997) = "101010110110000207"
  109.     daList(1998) = "100100110110150128"
  110.     daList(1999) = "100100101110000216"
  111.     daList(2000) = "110010010110000205"
  112.     daList(2001) = "110101001010140124"
  113.     daList(2002) = "110101001010000212"
  114.     daList(2003) = "110110100101000201"
  115.     daList(2004) = "010110101010120122"
  116.     daList(2005) = "010101101010000209"
  117.     daList(2006) = "101010101101170129"
  118.     daList(2007) = "001001011101000218"
  119.     daList(2008) = "100100101101000207"
  120.     daList(2009) = "110010010101150126"
  121.     daList(2010) = "101010010101000214"
  122.     daList(2011) = "101101001010000214"
  123.     AddYear = tYear
  124.     RunYue = False

  125.     If IsGetGl Then
  126.         AddMonth = Val(Mid(daList(AddYear), 15, 2))
  127.         AddDay = Val(Mid(daList(AddYear), 17, 2))
  128.         conDate = DateSerial(AddYear, AddMonth, AddDay)
  129.         AddDay = tDay
  130.         For i = 1 To tMonth - 1
  131.             AddDay = AddDay + 29 + Val(Mid(daList(tYear), i, 1))
  132.         Next i
  133.         'MsgBox DateDiff("d", conDate, Date)
  134.         setDate = DateAdd("d", AddDay - 1, conDate)
  135.         GetYLDate = setDate
  136.         tYear = Year(setDate)
  137.         tMonth = Month(setDate)
  138.         tDay = Day(setDate)
  139.         Exit Function
  140.     End If
  141. CHUSHIHUA:
  142.     AddMonth = Val(Mid(daList(AddYear), 15, 2))
  143.     AddDay = Val(Mid(daList(AddYear), 17, 2))
  144.     conDate = DateSerial(AddYear, AddMonth, AddDay)
  145.     setDate = DateSerial(tYear, tMonth, tDay)
  146.     getDay = DateDiff("d", conDate, setDate)
  147.     If getDay <  0 Then AddYear = AddYear - 1: GoTo CHUSHIHUA
  148.    ' addday = NearDay
  149.    AddDay = 1: AddMonth = 1
  150.     For i = 1 To getDay
  151.         AddDay = AddDay + 1
  152.         If AddDay = 30 + Mid(daList(AddYear), AddMonth, 1) Or (RunYue And AddDay = 30 + Mid(daList(AddYear), 13, 1)) Then
  153.             If RunYue = False And AddMonth = Val("&H" & Mid(daList(AddYear), 14, 1)) Then
  154.                 RunYue = True
  155.             Else
  156.                 RunYue = False
  157.                 AddMonth = AddMonth + 1
  158.             End If
  159.             AddDay = 1
  160.         End If

  161.     Next

  162.     md$ = "初一初二初三初四初五初六初七初八初九初十十一十二十三十四十五十六十七十八十九二十廿一廿二廿三廿四廿五廿六廿七廿八廿九三十"
  163.     dd$ = Mid(md$, (AddDay - 1) * 2 + 1, 2)
  164.     mm$ = Mid("正二三四五六七八九十寒腊", AddMonth, 1) + "月"
  165.     YouGetDate = DateSerial(AddYear, AddMonth, AddDay)
  166.     tiangan$ = "甲乙丙丁戊已庚辛壬癸"
  167.     dizhi$ = "子丑寅卯辰巳午未申酉戌亥"
  168.     Dim ganzhi(0 To 59) As String * 2
  169.     For i = 0 To 59
  170.      ganzhi(i) = Mid(tiangan$, (i Mod 10) + 1, 1) + Mid(dizhi$, (i Mod 12) + 1, 1)
  171.     'ff$ = ff$ + ganzhi(i)
  172.     Next i
  173.     'MsgBox ff$, , Len(ff$)
  174.     YLyear = ganzhi((AddYear - 4) Mod 60)
  175.     shu$ = "鼠牛虎兔龙蛇马羊猴鸡狗猪"
  176.     YLShuXing = Mid(shu$, ((AddYear - 4) Mod 12) + 1, 1)
  177.     If RunYue Then mm$ = "闰" + mm$

  178.     GetYLDate = mm$ + dd$

  179. End Function
复制代码
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
分享淘帖0 收藏收藏0 评分评分
您需要登录后才可以回帖 登录 | 立即注册


手机版|SKY外语计算机学习 ( 粤ICP备12031577 )    

GMT+8, 2024-12-22 00:34 , Processed in 0.111948 second(s), 27 queries .

回顶部