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

  • 11153
  • 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了。

 


或許您會對下列培訓課程感興趣:

  1. 2021/1/9:【針對遺留代碼加入單元測試的藝術】202101 - 台北
  2. 2021/1/10:【極速開發+】 202101 台北
  3. 2021/2/20~2021/2/21:【演化式設計】測試驅動開發與持續重構 202102

想收到第一手公開培訓課程資訊,或想詢問企業內訓、顧問、教練、諮詢服務的,請洽 Facebook 粉絲專頁:91敏捷開發之路