当前位置:   article > 正文

[修改]公历转农历

公历转农历
  1. '1901~2100
  2. Private Const ylData = "010010111101080131,010010101110000219,101001010111000208,010101001101050129,110100100110000216,110110010101000204,011001010101140125,010101101010000213,100110101101000202,010101011101020122," & _
  3. "010010101110000210,101001011011060130,101001001101000218,110100100101000206,110100100101150126,101101010101000214,010101101010000204,101011011010020123,100101011011000211,010010010111170201," & _
  4. "010010011011000220,101001001011000208,101101001011050128,011010100101000216,011011010100000205,101010110101140124,001010110110000213,100101010111000202,010100101111020123,010010010111000210," & _
  5. "011001010110060130,110101001010000217,111010100101000206,011010101001150126,010110101101000214,001010110110000204,100001101110130124,100100101110000211,110010001101170131,110010010101000219," & _
  6. "110101001010000208,110110001010160127,101101010101000215,010101101010000205,101001011011140125,001001011101000213,100100101101000202,110100101011020122,101010010101000210,101101010101070129," & _
  7. "011011001010000217,101101010101000206,010100110101150127,010011011010000214,101001011011000203,010001010111130124,010100101011000212,101010011010080131,111010010101000218,011010101010000208," & _
  8. "101011101010060128,101010110101000215,010010110110000205,101010101110040125,101001010111000213,010100100110000202,111100100110030121,110110010101000209,010110110101070130,010101101010000217," & _
  9. "100101101101000206,010011011101050127,010010101101000215,101001001101000203,110101001101040123,110100100101000211,110101010101080131,101101010100000218,101101101010000207,100101011010160128," & _
  10. "100101011011000216,010010011011000205,101010010111040125,101001001011000213,1011001001110A0202,011010100101000220,011011010100000209,101011110100060129,101010110110000217,100101010111000206," & _
  11. "010010101111050127,010010010111000215,011001001011000204,011101001010030123,111010100101000210,011010110101080131,010110101100000219,101010110110000207,100101101101050128,100100101110000216," & _
  12. "110010010110000205,110110010101040124,110101001010000212,110110100101000201,011101010101020122,010101101010000209,101010111011070129,001001011101000218,100100101101000207,110010101011050126," & _
  13. "101010010101000214,101101001010000203,101110101010040123,101011010101000210,010101011101090131,010010111010000219,101001011011000208,010100010111160128,010100101011000216,101010010011000205," & _
  14. "011110010101040125,011010101010000212,101011010101000201,010110110101020122,010010110110000210,100101101110060129,101001001110000217,110100100110000206,111010100110050126,110101010011000213," & _
  15. "010110101010000203,011101101010030123,100101101101000211,0100101010110B0131,010010101101000219,101001001101000208,110100001011160128,110100100101000215,110101010010000204,110111010100050124," & _
  16. "101101011010000212,010101101101000201,010101011011020122,010010011011000210,101001010111070130,101001001011000217,101010100101000206,101100100101150126,011011010010000214,101011011010000202," & _
  17. "010010110110130123,100100110111000211,010010011111080201,010010010111000219,011001001011000208,011010001010160128,111010100101000215,011010101010000204,101001101100140124,101010101110000212," & _
  18. "100100101110000202,110100101110030121,110010010110000209,110101010101070129,110101001010000217,110110100100000205,010111010101050126,010101101010000214,101001101100000203,010101011101040123," & _
  19. "010100101101000211,101010011011080131,101010010101000219,101101001010000207,101101101010060127,101011010101000215,010101011010000205,101010111010040124,101001011010000212,010100101011000202," & _
  20. "101100100111030122,011010010011000209,011100110011070129,011010101010000217,101011010101000206,010010110101150126,010010110110000214,101001010111000203,010101001110040124,110100010110000210," & _
  21. "111010010110080130,110101010010000218,110110101010000207,011010101010160127,010101101101000215,010010101110000205,101010011101040125,101000101101000212,110100010101000201,111100100101020121,110101010010000209"
  22. Private Const ylMd0 = "初一初二初三初四初五初六初七初八初九初十十一十二十三十四十五十六十七十八十九二十廿一廿二廿三廿四廿五廿六廿七廿八廿九三十 "
  23. Private Const ylMn0 = "正二三四五六七八九十冬腊"
  24. Private Const ylTianGan0 = "甲乙丙丁戊己庚辛壬癸"
  25. Private Const ylDiZhi0 = "子丑寅卯辰巳午未申酉戌亥"
  26. Private Const ylShu0 = "鼠牛虎兔龙蛇马羊猴鸡狗猪"
  27. '公历日期转农历
  28. Function GetNLDate(ByVal strDate As String) As String
  29. If Not IsDate(strDate) Then Return ""
  30. Dim setDate As Date
  31. Dim tYear, tMonth, tDay As Integer
  32. '
  33. setDate = CDate(strDate)
  34. tYear = Year(setDate) : tMonth = Month(setDate) : tDay = CDate(setDate).ToString("dd")
  35. '超出范围
  36. If tYear > 2100 OrElse tYear < 1901 Then Return ""
  37. '
  38. Dim daList(1) As String
  39. Dim conDate As Date
  40. Dim thisMonths As String
  41. Dim AddYear, AddMonth, AddDay, getDay, RunYue1, mDays, i, j As Integer
  42. Dim YLyear, YLShuXing As String
  43. Dim dd0, mm0 As String
  44. Dim ganzhi(59) As String
  45. Dim RunYue As Boolean
  46. Try
  47. '加载2年内的农历数据
  48. daList(0) = ylData.Substring((tYear - 1900 - 1) * 19, 18)
  49. daList(1) = ylData.Substring((tYear - 1900) * 19, 18)
  50. AddYear = tYear
  51. j = 1
  52. While True
  53. AddMonth = CInt(Mid(daList(j), 15, 2))
  54. AddDay = CInt(Mid(daList(j), 17, 2))
  55. conDate = DateSerial(AddYear, AddMonth, AddDay) '农历新年日期
  56. getDay = DateDiff(DateInterval.Day, conDate, setDate) + 1 '相差天数
  57. If getDay > 0 Then Exit While
  58. AddYear -= 1
  59. j -= 1
  60. End While
  61. thisMonths = Left(daList(j), 14)
  62. RunYue1 = Val("&H" & Right(thisMonths, 1)) '闰月月份
  63. If RunYue1 > 0 Then '有闰月
  64. thisMonths = Left(thisMonths, RunYue1) & Mid(thisMonths, 13, 1) & Mid(thisMonths, RunYue1 + 1)
  65. End If
  66. thisMonths = Left(thisMonths, 13)
  67. For i = 1 To 13 '计算天数
  68. mDays = 29 + CInt(Mid(thisMonths, i, 1))
  69. If getDay > mDays Then
  70. getDay = getDay - mDays
  71. Else
  72. If RunYue1 > 0 Then
  73. If i = RunYue1 + 1 Then RunYue = True
  74. If i > RunYue1 Then i = i - 1
  75. End If
  76. AddMonth = i
  77. AddDay = getDay
  78. Exit For
  79. End If
  80. Next
  81. dd0 = Mid(ylMd0, (AddDay - 1) * 2 + 1, 2)
  82. mm0 = Mid(ylMn0, AddMonth, 1) + "月"
  83. For i = 0 To 59
  84. ganzhi(i) = Mid(ylTianGan0, (i Mod 10) + 1, 1) + Mid(ylDiZhi0, (i Mod 12) + 1, 1)
  85. Next
  86. YLyear = ganzhi((AddYear - 4) Mod 60)
  87. YLShuXing = Mid(ylShu0, ((AddYear - 4) Mod 12) + 1, 1)
  88. If RunYue Then mm0 = "闰" & mm0
  89. Catch ex As Exception
  90. Return ""
  91. End Try
  92. Return "农历:" & YLyear & "(" & YLShuXing & ")年" & mm0 & dd0
  93. End Function
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/1017312
推荐阅读
相关标签
  

闽ICP备14008679号