程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> ChineseLunisolarCalendar 農歷日期

ChineseLunisolarCalendar 農歷日期

編輯:關於.NET

在.net2.0 的版本中的 System.Globalization.ChineseLunisolarCalendar 是針對中 國的日歷類,公歷與中國傳統農歷紀年之間的相互轉換,利用它可以計算天干地支等有關 農歷的信息。

使用

static ChineseLunisolarCalendar cCalendar = new ChineseLunisolarCalendar ();

cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28

cCalendar.MinSupportedDateTime  返回支持的最小日期,即1901-2-19

下面我們來實現公歷轉農歷。

/// <summary>
/// 根據公歷獲取農歷日期
/// </summary>
/// <param name="datetime">公歷日期</param>
/// <returns></returns>
public static string GetChineseDateTime(DateTime datetime)
{
     int lyear = cCalendar.GetYear(datetime);
     int lmonth = cCalendar.GetMonth(datetime);
     int lday = cCalendar.GetDayOfMonth(datetime);
     //獲取閏月, 0 則表示沒有閏月
     int leapMonth = cCalendar.GetLeapMonth(lyear);
     bool isleap = false;
     if (leapMonth > 0)
     {
         if (leapMonth == lmonth)
         {
             //閏月
             isleap = true;
             lmonth--;
         }
         else if (lmonth > leapMonth)
         {
             lmonth--;
         }
     }
     return string.Concat(GetLunisolarYear(lyear), "年", isleap ? " 閏" : string.Empty, GetLunisolarMonth(lmonth), "月", GetLunisolarDay (lday));
}

測試的結果:

傳入日期:2010-3-4

返回農歷:庚寅[虎]年正月十九

可以滿足簡單的需求啦。

其他代碼也附上:

#region 農歷年
/// <summary>
/// 十天干
/// </summary>
private static string[] tiangan = { "甲", "乙", "丙", "丁", "戊",  "己", "庚", "辛", "壬", "癸" };
/// <summary>
/// 十二地支
/// </summary>
private static string[] dizhi = { "子", "丑", "寅", "卯", "辰", " 巳", "午", "未", "申", "酉", "戌", "亥" };
/// <summary>
/// 十二生肖
/// </summary>
private static string[] shengxiao = { "鼠", "牛", "虎", "免",  "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬" };

/// <summary>
/// 返回農歷天干地支年 
/// </summary>
/// <param name="year">農歷年</param>
/// <returns></returns>
public static string GetLunisolarYear(int year)
{
     if (year > 3)
     {
         int tgIndex = (year - 4) % 10;
         int dzIndex = (year - 4) % 12;
         return string.Concat(tiangan[tgIndex], dizhi[dzIndex],  "[", shengxiao[dzIndex], "]");
     }
     throw new ArgumentOutOfRangeException("無效的年份!");
}       

#endregion

#region 農歷月
/// <summary>
/// 農歷月
/// </summary>
private static string[] months = { "正", "二", "三", "四", "五",  "六", "七", "八", "九", "十", "十一", "十二(臘)" };

/// <summary>
/// 返回農歷月
/// </summary>
/// <param name="month">月份</param>
/// <returns></returns>
public static string GetLunisolarMonth(int month)
{
     if (month < 13 && month > 0)
     {
         return months[month - 1];
     }
     throw new ArgumentOutOfRangeException("無效的月份!");
}

#endregion

#region 農歷日
/// <summary>
/// 
/// </summary>
private static string[] days1 = { "初", "十", "廿", "三" };
/// <summary>
/// 日
/// </summary>
private static string[] days = { "一", "二", "三", "四", "五", " 六", "七", "八", "九", "十" };

/// <summary>
/// 返回農歷日
/// </summary>
/// <param name="day"></param>
/// <returns></returns>
public static string GetLunisolarDay(int day)
{
     if (day > 0 && day < 32)
     {
         if (day != 20 && day != 30)
         {
             return string.Concat(days1[(day - 1) / 10],  days[(day - 1) % 10]);
         }
         else
         {
             return string.Concat(days[(day - 1) / 10],  days1[1]);
         }
     }
     throw new ArgumentOutOfRangeException("無效的日!");
}
#endregion

還有一個根據日期獲取生肖的代碼:

/// <summary>
/// 返回生肖
/// </summary>
/// <param name="datetime">公歷日期</param>
/// <returns></returns>
public static string GetShengXiao(DateTime datetime)
{
     return shengxiao[cCalendar.GetTerrestrialBranch (cCalendar.GetSexagenaryYear(datetime)) - 1];
}

一切都變的簡單了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved