贴在这里。
Function WesternYearToGanZhi(ByVal WesternYear As Integer) As String
Dim tempGan, tempZhi As Byte
'下面的代码用来计算下标
If WesternYear >= 4 Then '如果年份在公元4年及以后,是这样算滴。
tempGan = (WesternYear - 4) Mod 10
tempZhi = (WesternYear - 4) Mod 12
Elseif WesternYear = 3 Then
Return "癸亥" : Exit Function
ElseIf WesternYear = 2 Then '公元2年,不用算了,是壬戌年。
Return "壬戌" : Exit Function
ElseIf WesternYear = 1 Then '公元元年,是辛酉年。
Return "辛酉" : Exit Function
ElseIf WesternYear < 0 Then '如果年份是公元前,这个略复杂,不信去验证一下。
tempGan = 9 - ((2-WesternYear) Mod 10)
tempZhi = 11 - ((2-WesternYear) Mod 12)
Else
Throw New Exception("There was no year 0!") '抛出异常,因为没有公元0年。
End If
'下标算好了,祭出数组。甲(子)对应的下标为0,癸(亥)对应的下标为9(11)。
Dim Gans As String() = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}
Dim Zhis As String() = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}
Return Gans(tempGan) & Zhis(tempZhi) '天干在前,地支在后,并起来。
End Function
转载请注明原文地址: https://ju.6miu.com/read-1297673.html