帮我写一个 可用的asp农历函数
时间: 2024-11-10 14:26:51 浏览: 17
excel中161个VBA_自定义函数(超级实用)
当然可以!以下是一个基于提供的 `getLunar` JavaScript 函数转换为 ASP(Active Server Pages)的农历函数。这个函数将返回指定日期的农历信息。
```asp
<%
Function GetLunar(dateString)
Dim nyear, nmonth, nday, nwday, nhrs, nmin, nsec
Dim lmonth, lday, lleap
Dim noww, lObj, lunar_month_day
' 解析输入日期
noww = CDate(dateString)
' 初始化变量
nyear = Year(noww)
nmonth = Month(noww)
nday = Day(noww)
nwday = Weekday(noww, vbSunday) ' 星期几,从周日开始计数
nhrs = Hour(noww)
nmin = Minute(noww)
nsec = Second(noww)
' 调用农历计算函数
Set lObj = New Lunar
Call lObj.Init(nyear, nmonth - 1, nday)
' 格式化农历月份和日期
lmonth = GetcMon(lObj.Month)
If lObj.IsLeap Then
lmonth = "闰" & lmonth
End If
lday = GetcDay(lObj.Day)
' 返回农历结果
lunar_month_day = lmonth & "月" & lday
GetLunar = lunar_month_day
End Function
' 辅助函数
Function cweekday(wday)
Dim hzWeek
hzWeek = Array("日", "一", "二", "三", "四", "五", "六", "日")
cweekday = hzWeek(wday)
End Function
Function shapetime(vhrs, vmin, vsec)
If vsec < 10 Then vsec = "0" & vsec
If vmin < 10 Then vmin = "0" & vmin
If vhrs < 10 Then vhrs = "0" & vhrs
shapetime = vhrs & ":" & vmin & ":" & vsec
End Function
Function GetcDay(d)
Dim nStr1, nStr2, s
nStr1 = Array("", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二")
nStr2 = Array("初", "十", "廿", "卅", "□")
Select Case d
Case 10
s = "初十"
Case 20
s = "二十"
Case 30
s = "三十"
Case Else
s = nStr2(Fix(d / 10))
s = s & nStr1(d Mod 10)
End Select
GetcDay = s
End Function
Function GetcMon(m)
If m = 1 Then
GetcMon = "正"
Else
GetcMon = nStr1(m)
End If
End Function
' 农历类
Class Lunar
Private lunarInfo
Private year, month, day, isLeap
Private Sub Class_Initialize
ReDim lunarInfo(152)
lunarInfo = Array(&H4BD8, &H4AE0, &HA570, &H54D5, &HD260, &HD950, &H16554, &H56A0, &H9AD0, &H55D2, _
&H4AE0, &HA5B6, &HA4D0, &HD250, &H1D255, &HB540, &HD6A0, &HADA2, &H95B0, &H14977, _
&H4970, &HA4B0, &HB4B5, &H6A50, &H6D40, &H1AB54, &H2B60, &H9570, &H52F2, &H4970, _
&H6566, &HD4A0, &HEA50, &H6E95, &H5AD0, &H2B60, &H186E3, &H92E0, &H1C8D7, &HC950, _
&HD4A0, &H1D8A6, &HB550, &H56A0, &H1A5B4, &H25D0, &H92D0, &HD2B2, &HA950, &HB557, _
&H6CA0, &HB550, &H15355, &H4DA0, &HA5B0, &H14573, &H52B0, &HA9A8, &HE950, &H6AA0, _
&HAEA6, &HAB50, &H4B60, &HAAE4, &HA570, &H5260, &HF263, &HD950, &H5B57, &H56A0, _
&H96D0, &H4DD5, &H4AD0, &HA4D0, &HD4D4, &HD250, &HD558, &HB540, &HB6A0, &H195A6, _
&H95B0, &H49B0, &HA974, &HA4B0, &HB27A, &H6A50, &H6D40, &HAF46, &HAB60, &H9570, _
&H4AF5, &H4970, &H64B0, &H74A3, &HEA50, &H6B58, &H5AC0, &HAB60, &H96D5, &H92E0, _
&HC960, &HD954, &HD4A0, &HDA50, &H7552, &H56A0, &HABB7, &H25D0, &H92D0, &HCAB5, _
&HA950, &HB4A0, &HBAA4, &HAD50, &H55D9, &H4BA0, &HA5B0, &H15176, &H52B0, &HA930, _
&H7954, &H6AA0, &HAD50, &H5B52, &H4B60, &HA6E6, &HA4E0, &HD260, &HEA65, &HD530, _
&H5AA0, &H76A3, &H96D0, &H4BD7, &H4AD0, &HA4D0, &H1D0B6, &HD250, &HD520, &HDD45, _
&HB5A0, &H56D0, &H55B2, &H49B0, &HA577, &HA4B0, &HAA50, &H1B255, &H6D20, &HADA0, _
&H14B63)
year = 0
month = 0
day = 0
isLeap = False
End Sub
Public Sub Init(y, m, d)
Dim offset, i, temp, leap
' 计算偏移天数
offset = DateDiff("d", CDate("1900-01-31"), CDate(y & "-" & (m + 1) & "-" & d))
For i = 1900 To 2050 Step 1
If offset > 0 Then
temp = LYearDays(i)
offset = offset - temp
Else
Exit For
End If
Next
If offset < 0 Then
offset = offset + temp
i = i - 1
End If
year = i
leap = LeapMonth(year)
isLeap = False
For i = 1 To 13 Step 1
If leap > 0 And i = (leap + 1) And Not isLeap Then
i = i - 1
isLeap = True
temp = LeapDays(year)
Else
temp = MonthDays(year, i)
End If
If isLeap And i = (leap + 1) Then
isLeap = False
End If
If offset > 0 Then
offset = offset - temp
Else
Exit For
End If
Next
If offset = 0 And leap > 0 And i = leap + 1 Then
If isLeap Then
isLeap = False
Else
isLeap = True
i = i - 1
End If
End If
If offset < 0 Then
offset = offset + temp
i = i - 1
End If
month = i
day = offset + 1
End Sub
Private Function LYearDays(y)
Dim i, sum
sum = 348
For i = &H8000 To &H8 Step -1
If (lunarInfo(y - 1900) And i) Then
sum = sum + 1
End If
Next
LYearDays = sum + LeapDays(y)
End Function
Private Function LeapDays(y)
If LeapMonth(y) Then
LeapDays = IIf((lunarInfo(y - 1900) And &H10000), 30, 29)
Else
LeapDays = 0
End If
End Function
Private Function LeapMonth(y)
LeapMonth = (lunarInfo(y - 1900) And &HF)
End Function
Private Function MonthDays(y, m)
MonthDays = IIf((lunarInfo(y - 1900) And (&H10000 Shr m)), 30, 29)
End Function
Public Property Get Year
Year = Me.year
End Property
Public Property Get Month
Month = Me.month
End Property
Public Property Get Day
Day = Me.day
End Property
Public Property Get IsLeap
IsLeap = Me.isLeap
End Property
End Class
' 测试
Response.Write(GetLunar("2023-11-03"))
%>
```
### 使用说明
1. 将上述代码保存为一个 `.asp` 文件,例如 `lunar.asp`。
2. 在支持 ASP 的服务器上运行该文件。
3. 访问该文件的 URL,例如 `http://yourserver/lunar.asp`,将会输出指定日期的农历信息。
希望这对你有帮助!如果有任何问题,请随时告诉我。
阅读全文