校正后的公历农历互转函数
'阴阳历转换 和 阴阳历生日
'Version: 1.1 2005-9-1
'Author: James Zhuang
'Lunar(SolarDate[, Part = 0 | 1 | 2 | 3]) 阳历转换成阴历
' Part = 0, all; Part = 1, lunar year; Part = 2, lunar month; Part = 3, lunar day
'Solar(LunarDate[, LunarMonth = 0 | 1]) 阴历转换成阳历
Type ConvDataA
leapmonth As Integer
month(1 To 13) As Integer
sp_month As Integer 'Solar month of Spring Festival
sp_day As Integer 'Solar day of Spring Festival
End Type
Private Function LunarData(q_year) As ConvDataA
Dim d As Long
Dim month(1 To 13) As Integer
'1901-2100
LunarCal = Array( _
&H4AE53, &HA5748, &H5526BD, &HD2650, &HD9544, &H46AAB9, &H56A4D, &H9AD42, &H24AEB6, &H4AE4A, _
&H6A4DBE, &HA4D52, &HD2546, &H5D52BA, &HB544E, &HD6A43, &H296D37, &H95B4B, &H749BC1, &H49754, _
&HA4B48, &H5B25BC, &H6A550, &H6D445, &H4ADAB8, &H2B64D, &H95742, &H2497B7, &H4974A, &H664B3E, _
&HD4A51, &HEA546, &H56D4BA, &H5AD4E, &H2B644, &H393738, &H92E4B, &H7C96BF, &HC9553, &HD4A48, _
&H6DA53B, &HB554F, &H56A45, &H4AADB9, &H25D4D, &H92D42, &H2C95B6, &HA954A, &H7B4ABD, &H6CA51, _
&HB5546, &H555ABB, &H4DA4E, &HA5B43, &H352BB8, &H52B4C, &H8A953F, &HE9552, &H6AA48, &H7AD53C, _
&HAB54F, &H4B645, &H4A5739, &HA574D, &H52642, &H3E9335, &HD9549, &H75AABE, &H56A51, &H96D46, _
&H54AEBB, &H4AD4F, &HA4D43, &H4D26B7, &HD254B, &H8D52BF, &HB5452, &HB6A47, &H696D3C, &H95B50, _
&H49B45, &H4A4BB9, &HA4B4D, &HAB25C2, &H6A554, &H6D449, &H6ADA3D, &HAB651, &H95746, &H5497BB, _
&H4974F, &H64B44, &H36A537, &HEA54A, &H86B2BF, &H5AC53, &HAB647, &H5936BC, &H92E50, &HC9645, _
&H4D4AB8, &HD4A4C, &HDA541, &H25AAB6, &H56A49, &H7AADBD, &H25D52, &H92D47, &H5C95BA, &HA954E, _
&HB4A43, &H4B5537, &HAD54A, &H955ABF, &H4BA53, &HA5B48, &H652BBC, &H52B50, &HA9345, &H474AB9, _
&H6AA4C, &HAD541, &H24DAB6, &H4B64A, &H6A573D, &HA4E51, &HD2646, &H5E933A, &HD534D, &H5AA43, _
&H36B537, &H96D4B, &HB4AEBF, &H4AD53, &HA4D48, &H6D25BC, &HD254F, &HD5244, &H5DAA38, &HB5A4C, _
&H56D41, &H24ADB6, &H49B4A, &H7A4BBE, &HA4B51, &HAA546, &H5B52BA, &H6D24E, &HADA42, &H355B37, _
&H9374B, &H8497C1, &H49753, &H64B48, &H66A53C, &HEA54F, &H6AA44, &H4AB638, &HAAE4C, &H92E42, _
&H3C9735, &HC9649, &H7D4ABD, &HD4A51, &HDA545, &H55AABA, &H56A4E, &HA6D43, &H452EB7, &H52D4B, _
&H8A95BF, &HA9553, &HB4A47, &H6B553B, &HAD54F, &H55A45, &H4A5D38, &HA5B4C, &H52B42, &H3A93B6, _
&H69349, &H7729BD, &H6AA51, &HAD546, &H54DABA, &H4B64E, &HA5743, &H452738, &HD164A, &H8E933E, _
&HD5252, &HDAA47, &H66B53B, &H56D4F, &H4AE45, &H4A4EB9, &HA2D4C, &HD1541, &H2D92B5, &HD5349)
startyear = 1901
ng = LunarCal(q_year - startyear)
d = &H100000
LunarData.leapmonth = Int(ng / d)
ng = ng Mod d
d = &H80
mdata = Int(ng / d)
ng = ng Mod d
d = &H20
LunarData.sp_month = Int(ng / d)
LunarData.sp_day = ng Mod d
d = &H1000
i = 1
Do
LunarData.month(i) = 29 + Int(mdata / d)
mdata = mdata Mod d
If d = 1 Then Exit Do
d = d / 2
i = i + 1
Loop
If LunarData.leapmonth = 0 Then
LunarData.month(13) = 0
Else
b = LunarData.month(LunarData.leapmonth + 1)
For i = LunarData.leapmonth + 1 To 12
LunarData.month(i) = LunarData.month(i + 1)
Next i
LunarData.month(13) = b
End If
End Function
Function lunar(Solar_date As Date, Optional Part As Integer = 0) As String
'Part = 0, all; Part = 1, lunar year; Part = 2, lunar month; Part = 3, lunar day
Dim a As ConvDataA
l_year = Year(Solar_date)
a = LunarData(l_year)
sp_date = DateSerial(l_year, a.sp_month, a.sp_day)
If sp_date > Solar_date Then
l_year = l_year - 1
a = LunarData(l_year)
sp_date = DateSerial(l_year, a.sp_month, a.sp_day)
End If
l_day = Solar_date - sp_date
l_month = 1
IS_lunar_leapmonth = False
y = a.month(l_month)
Do While l_day >= y
l_day = l_day - y
If l_month = a.leapmonth Then IS_lunar_leapmonth = (Not IS_lunar_leapmonth)
If IS_lunar_leapmonth Then
y = a.month(13) '???
Else
l_month = l_month + 1
y = a.month(l_month)
End If
Loop
l_day = l_day + 1
lunar = l_year & "-" & l_month & "-" & l_day
If IS_lunar_leapmonth Then lunar = lunar & "-L"
lunar = Choose(Part + 1, lunar, l_year, l_month, l_day)
End Function
Function solar(Lunar_date, Optional IS_lunar_leapmonth As Integer = 0) As String
'IS_lunar_leapmonth = 0, No leap month; IS_lunar_leapmonth = 1, is leap month
Dim a As ConvDataA
Lunar_date = Split(Lunar_date, "-")
s_year = Lunar_date(0)
For Each C In Lunar_date
If C = "L" Then IS_lunar_leapmonth = 1
Next
a = LunarData(s_year)
sp_date = DateSerial(s_year, a.sp_month, a.sp_day)
If Lunar_date(1) <> a.leapmonth Then IS_lunar_leapmonth = 0
x = Lunar_date(2)
tm = Lunar_date(1) + IS_lunar_leapmonth - 1
For i = 1 To tm
x = x + a.month(i)
If i = a.leapmonth And IS_lunar_leapmonth = 0 Then
x = x + a.month(13)
End If
Next
s_date = sp_date + x - 1
solar = s_date
End Function
Function lunarbirth(Solar_birthday As Date, Optional Inquire_year As Integer) As String
If Inquire_year = 0 Then
Inquire_year = Left(lunar(Now), 4)
lunarbirth = solar(Inquire_year & Mid(lunar(Solar_birthday), 5, 10))
If CDate(lunarbirth) < Now - 1 Then Inquire_year = Inquire_year + 1
End If
lunarbirth = solar(Inquire_year & Mid(lunar(Solar_birthday), 5, 10))
End Function
Function solarbirth(Solar_birthday As Date, Optional Inquire_year As Integer) As String
If Inquire_year = 0 Then
Inquire_year = Year(Now)
solarbirth = DateSerial(Inquire_year, month(Solar_birthday), Day(Solar_birthday))
If CDate(solarbirth) < Now - 1 Then Inquire_year = Inquire_year + 1
End If
solarbirth = DateSerial(Inquire_year, month(Solar_birthday), Day(Solar_birthday))
End Function
'校正:YZC51
补充内容 (2018-11-9 22:49):
重新矫正的数据,在31楼! |