周末試着做了一個公歷轉農歷的例子,執行結果如下:
代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace AlgorithmPractice { public partial class 公歷轉農歷 : Form { public 公歷轉農歷() { InitializeComponent(); } private void 公歷轉農歷_Load(object sender, EventArgs e) { BindYear(); this.cbYear.SelectedItem = DateTime.Now.Year; this.cbMonth.SelectedItem = DateTime.Now.Month; this.cbDay.SelectedItem = DateTime.Now.Day; btnChange_Click(sender,e); } //綁定年份 public void BindYear() { List<int> arrYear = new List<int>(); for (int i = 0; i <= 2101 - 1901; i++) { arrYear.Insert(i, 1901 + i); } this.cbYear.DataSource = arrYear; } //月份隨年份而變 private void cbYear_SelectedIndexChanged(object sender, EventArgs e) { int[] year1901 = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; int[] year2101 = { 1 }; int[] yearOther = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; if (this.cbYear.Text == "1901") { this.cbMonth.DataSource = year1901; } else if (this.cbYear.Text == "2101") { this.cbMonth.DataSource = year2101; } else { this.cbMonth.DataSource = yearOther; } } //開始轉換 private void btnChange_Click(object sender, EventArgs e) { try { string date = this.cbYear.Text + "-" + cbMonth.Text + "-" + cbDay.Text; DateTime dt = Convert.ToDateTime(date); this.lblNongLi.Text = SolarToChineseLunisolarDate(dt); } catch (Exception ex) { MessageBox.Show(ex.Message); } } /// <summary> /// 公歷轉為農歷的函數 /// </summary> /// <param name="solarDateTime">公歷日期</param> /// <returns>農歷的日期</returns> public static string SolarToChineseLunisolarDate(DateTime solarDateTime) { //微軟的ChineseLunisolarCalendar 方法支持時間范圍是[1901-2-19,2101-1-28] System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar(); int year = cal.GetYear(solarDateTime);//農歷年份 int month = cal.GetMonth(solarDateTime);//有閏月時該值可能為13,即leapMonth <= month ? month - 1 : month表示實際農歷月份 int day = cal.GetDayOfMonth(solarDateTime);//農歷天數 int leapMonth = cal.GetLeapMonth(year);//此年份閏幾月,閏n月則返回n+1,如閏4月返回值為5;沒有閏月返回0 String impday = "";//一年中的農歷節日 String leapMonthStr = leapMonth > 0 ? "(閏" + (leapMonth - 1).ToString() + "月)" : "";//閏月份 int monthtrue = leapMonth > 0 && leapMonth <= month ? month - 1 : month;//把閏月計算進去之后真正的農歷月份 if (monthtrue == 1 && day == 1) { impday = "春節"; } else if (monthtrue == 5 && day == 5) { impday = "端午"; } else if (monthtrue == 8 && day == 15) { impday = "中秋"; } String month2 = String.Format("{0}{1}月", month == leapMonth ? "閏" : "" , "無正二三四五六七八九十冬臘"[monthtrue] ); String day2 = string.Format("{0}{1}" , "初十廿三"[day == 10 ? 0 : day / 10] , "十一二三四五六七八九"[day % 10] ); String date = year + "年" + month2 + day2; if (impday != "") { return date + leapMonthStr + " " + "祝您" + impday + "節快樂 ^-^"; } else return date + leapMonthStr; } //日子隨月份改變 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.cbYear.Text.Trim())) { MessageBox.Show("年份不能為空!"); return; } try { int[] month31 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }; int[] month30 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 }; int[] month29 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 }; int[] month28 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 }; int[] month190102 = { 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 }; int[] month210101 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28 }; String strmonth = this.cbMonth.Text.Trim(); String stryear = this.cbYear.Text.Trim(); if (stryear == "1901" && strmonth == "2") { this.cbDay.DataSource =month190102; } else if (stryear == "2101" && strmonth == "1") { this.cbDay.DataSource = month210101; } else { if (strmonth == "1" || strmonth == "3" || strmonth == "5" || strmonth == "7" || strmonth == "8" || strmonth == "10" || strmonth == "12") { this.cbDay.DataSource = month31; } else if (strmonth == "4" || strmonth == "9" || strmonth == "6" || strmonth == "11") { this.cbDay.DataSource = month30; } else if (strmonth == "2") { if (System.DateTime.IsLeapYear(Convert.ToInt32(this.cbYear.Text.Trim())) == true) { this.cbDay.DataSource = month29;//閏年 } else { this.cbDay.DataSource = month28; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } } }