Code:
Private Function getInt(ByRef fNumber As Double) As Double
If fNumber < -0.0000001 Then
getInt = Math.Ceiling(fNumber - 0.0000001)
Else
getInt = Math.Floor(fNumber + 0.0000001)
End If
End Function
Private Sub SolarToLunar()
' convert Solar year from 622 to 2500
Dim jd As Double
Dim j, L, n As Double
Dim d, m, y As Integer
Dim theDay As Integer
' Solar day
d = Val(SolarDay.Text)
'get the number of Solar month
m = SolarMonth.SelectedIndex + 1
' Solar year
y = Val(SolarYear.Text)
If (y > 1582) Or ((y = 1582) And (m > 10)) Or ((y = 1582) And (m = 10) And (d > 14)) Then
jd = getInt((1461 * (y + 4800 + getInt((m - 14) / 12))) / 4) + getInt((367 * (m - 2 - 12 * (getInt((m - 14) / 12)))) / 12) - getInt((3 * (getInt((y + 4900 + getInt((m - 14) / 12)) / 100))) / 4) + d - 32075
Else
jd = 367 * y - getInt((7 * (y + 5001 + getInt((m - 9) / 7))) / 4) + getInt((275 * m) / 9) + d + 1729777
End If
' Solar year >= 622
If jd < 1948440 Then
DateMinError()
Exit Sub
End If
' Solar year <= 2500
If jd > 2621734 Then
DateMaxError()
Exit Sub
End If
'day of the week
theDay = jd Mod 7
lblDay.Text = WeekDays(theDay)
L = jd - 1948440 + 10632
n = getInt((L - 1) / 10631)
L = L - 10631 * n + 354
j = (getInt((10985 - L) / 5316)) * (getInt((50 * L) / 17719)) + (getInt(L / 5670)) * (getInt((43 * L) / 15238))
L = L - (getInt((30 - j) / 15)) * (getInt((17719 * j) / 50)) - (getInt(j / 16)) * (getInt((15238 * j) / 43)) + 29
m = Int(getInt((24 * L) / 709))
d = Int(L - getInt((709 * m) / 24))
y = Int(30 * n + j - 30)
' display Lunar date
LunarDay.Text = Str(d)
LunarMonth.Text = LunarMonths(m - 1)
LunarYear.Text = Str(y)
ShowMoonPhase()
If d = 1 Then
lblAge.Text = Str(d) & " day"
Else
lblAge.Text = Str(d) & " days"
End If
End Sub