[ASP.NET]民國年與農曆年類別

  • 16207
  • 0

[ASP.NET]民國年與農曆年類別

這又是一篇很久之前從保哥那邊挖到的寶藏,原文在這:找到民國(TaiwanCalendar)與農曆(TaiwanLunisolarCalendar)的日曆類別

由於程式開發人員的腦容量,都透過網路跟硬碟在放的,每次我總是會忘記類別名稱,這邊隨手做個記錄。

在這邊也感謝保哥的無私分享。(如有不妥請留言告知,謝謝)

 

另外一篇參考是來自Anita 的.NET 世界.NET 可以算出農曆嗎?

利用TaiwanLunisolarCalendar()的method去算天干地支的index,只需要再用string array去抓對應的天干地支出來即可。

兩個類別都是在System.Globalization底下,

  1. TaiwanCalendar :「民國年」
  2. TaiwanLunisolarCalendar :「農曆」

 

.aspx:

 


<%@ Page AutoEventWireup="true" CodeFile="TaiwanCalendarTest.aspx.cs" Inherits="TaiwanCalendarTest"
    Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>未命名頁面</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="民國年"></asp:Label>
        <asp:TextBox ID="txtYear" runat="server"></asp:TextBox>
        <asp:Label ID="Label2" runat="server" Text="民國月"></asp:Label>
        <asp:TextBox ID="txtMonth" runat="server"></asp:TextBox>
        <asp:Label ID="Label3" runat="server" Text="民國日"></asp:Label>
        <asp:TextBox ID="txtDay" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label4" runat="server" Text="農曆年"></asp:Label>
        <asp:TextBox ID="txtLunisolarYear" runat="server"></asp:TextBox>
        <asp:Label ID="Label5" runat="server" Text="農曆月"></asp:Label>
        <asp:TextBox ID="txtLunisolarMonth" runat="server"></asp:TextBox>
        <asp:Label ID="Label6" runat="server" Text="農曆日"></asp:Label>
        <asp:TextBox ID="txtLunisolarDay" runat="server"></asp:TextBox>
        
        <br />
        <asp:Label ID="Label7" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

.cs:

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;


public partial class TaiwanCalendarTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime now = DateTime.Now;
        TaiwanCalendar tc = new TaiwanCalendar();

        this.txtYear.Text = tc.GetYear(now).ToString();
        this.txtMonth.Text = tc.GetMonth(now).ToString();
        this.txtDay.Text = tc.GetDayOfMonth(now).ToString();

        TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar();
        this.txtLunisolarYear.Text = tlc.GetYear(now).ToString();
        this.txtLunisolarMonth.Text = tlc.GetMonth(now).ToString();
        this.txtLunisolarDay.Text = tlc.GetDayOfMonth(now).ToString();


        TaiwanLunisolarCalendar cal = new TaiwanLunisolarCalendar();        

        String 天干 = "0甲乙丙丁戊己庚辛壬癸";
        String 地支 = "0子丑寅卯辰巳午未申酉戌亥";

        int lun60Year = cal.GetSexagenaryYear(now);
        int 天干Year = cal.GetCelestialStem(lun60Year);
        int 地支Year = cal.GetTerrestrialBranch(lun60Year);

        int lunMonth = cal.GetMonth(now);
        int leapMonth = cal.GetLeapMonth(cal.GetYear(now));

        if (leapMonth > 0 && lunMonth >= leapMonth)
        {
            lunMonth -= 1;
        }

        int lunDay = cal.GetDayOfMonth(now);

        this.Label7.Text= (String.Format("農曆:{0}年{1}月{2}日",
           天干[天干Year].ToString() + 地支[地支Year].ToString(), lunMonth, lunDay));  
    }

}

結果:總算不需要在用西元年-1911了。

 


blog 與課程更新內容,請前往新站位置:http://tdd.best/