函式簽名
Function GetZodiac(birthDate)
' birthDate: Date 型別或可轉日期的字串
' 回傳: 星座中文名稱 (字串),失敗回傳空字串
End Function
完整實作
<%
Function GetZodiac(birthDate)
Dim m, d, key, zodiacs, i, z, startK, endK
GetZodiac = ""
' 輸入驗證
If birthDate = "" Or Not IsDate(birthDate) Then Exit Function
m = Month(birthDate)
d = Day(birthDate)
If m < 10 Then m = "0" & m
If d < 10 Then d = "0" & d
key = m & d ' 格式 MMDD,如 0321
' 星座區間表:名稱*起始*結束 (MMDD)
' 摩羯座跨年 (12/22~01/19) 特殊處理:預設值設為摩羯座
zodiacs = "水瓶座*0120*0218#雙魚座*0219*0320#白羊座*0321*0419#" & _
"金牛座*0420*0520#雙子座*0521*0621#巨蟹座*0622*0722#" & _
"獅子座*0723*0822#處女座*0823*0922#天秤座*0923*1023#" & _
"天蠍座*1024*1122#射手座*1123*1221#"
GetZodiac = "摩羯座" ' 跨年區間預設
Dim arr, bounds
arr = Split(zodiacs, "#")
For i = 0 To UBound(arr) - 1
bounds = Split(arr(i), "*")
' bounds(0)=名稱, bounds(1)=起始, bounds(2)=結束
If key >= bounds(1) And key <= bounds(2) Then
GetZodiac = bounds(0)
Exit For
End If
Next
End Function
%>
使用範例
<%
Dim testDates, i
testDates = Array("1990-01-15", "1990-02-18", "1990-03-21", "1990-12-25")
For i = 0 To UBound(testDates)
Response.Write testDates(i) & " → " & GetZodiac(testDates(i)) & "
"
Next
' 輸出:
' 1990-01-15 → 摩羯座
' 1990-02-18 → 水瓶座
' 1990-03-21 → 白羊座
' 1990-12-25 → 摩羯座
%>
邊界日期對照表 (陽曆)
| 星座 | 起始 | 結束 | 備註 |
| 摩羯座 | 12/22 | 01/19 | 跨年,預設值 |
| 水瓶座 | 01/20 | 02/18 | |
| 雙魚座 | 02/19 | 03/20 | |
| 白羊座 | 03/21 | 04/19 | |
| 金牛座 | 04/20 | 05/20 | |
| 雙子座 | 05/21 | 06/21 | |
| 巨蟹座 | 06/22 | 07/22 | |
| 獅子座 | 07/23 | 08/22 | |
| 處女座 | 08/23 | 09/22 | |
| 天秤座 | 09/23 | 10/23 | |
| 天蠍座 | 10/24 | 11/22 | |
| 射手座 | 11/23 | 12/21 | |
擴充:同時回傳英文名與日期區間
Function GetZodiacEx(birthDate)
Dim m, d, key, zodiacs, i, z, bounds, result
Set result = Server.CreateObject("Scripting.Dictionary")
result.Add "name", "" : result.Add "en", "" : result.Add "range", ""
If birthDate = "" Or Not IsDate(birthDate) Then
Set GetZodiacEx = result : Exit Function
End If
m = Month(birthDate) : d = Day(birthDate)
If m < 10 Then m = "0" & m
If d < 10 Then d = "0" & d
key = m & d
' 名稱*英文*起始*結束
zodiacs = "摩羯座*Capricorn*1222*0119#水瓶座*Aquarius*0120*0218#" & _
"雙魚座*Pisces*0219*0320#白羊座*Aries*0321*0419#" & _
"金牛座*Taurus*0420*0520#雙子座*Gemini*0521*0621#" & _
"巨蟹座*Cancer*0622*0722#獅子座*Leo*0723*0822#" & _
"處女座*Virgo*0823*0922#天秤座*Libra*0923*1023#" & _
"天蠍座*Scorpio*1024*1122#射手座*Sagittarius*1123*1221#"
result("name") = "摩羯座"
result("en") = "Capricorn"
result("range") = "12/22-01/19"
Dim arr : arr = Split(zodiacs, "#")
For i = 0 To UBound(arr) - 1
bounds = Split(arr(i), "*")
If key >= bounds(2) And key <= bounds(3) Then
result("name") = bounds(0)
result("en") = bounds(1)
result("range") = Left(bounds(2),2) & "/" & Right(bounds(2),2) & "-" & Left(bounds(3),2) & "/" & Right(bounds(3),2)
Exit For
End If
Next
Set GetZodiacEx = result
End Function
' 用法:
Dim z : Set z = GetZodiacEx("1995-07-15")
Response.Write z("name") & " (" & z("en") & ") " & z("range")
' 輸出: 巨蟹座 (Cancer) 06/22-07/22