萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 日期轉星座函式:Astro(birthDate) 回傳 12 星座中文名稱

函式簽名

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/2201/19跨年,預設值
水瓶座01/2002/18
雙魚座02/1903/20
白羊座03/2104/19
金牛座04/2005/20
雙子座05/2106/21
巨蟹座06/2207/22
獅子座07/2308/22
處女座08/2309/22
天秤座09/2310/23
天蠍座10/2411/22
射手座11/2312/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
copyright © 萬盛學電腦網 all rights reserved