public static string useTaiwanLC(DateTime date) { string TeanGean = "甲乙丙丁戊己庚辛壬癸"; string DeGe = "子丑寅卯辰巳午未申酉戌亥"; string CAnimal = "鼠牛虎兔龍蛇馬羊猴雞狗豬"; TaiwanLunisolarCalendar Tlc = new TaiwanLunisolarCalendar(); //DateTime dtNow = new DateTime.Now();//拿今年的日期 //DateTime.Now.AddYears; int lun60Year = Tlc.GetSexagenaryYear(date.AddYears(0)); // int lun60Year = Tlc.GetSexagenaryYear(DateTime.Now.AddYears(0)); int TeanGeanYear = Tlc.GetCelestialStem(lun60Year) - 1; int DeGeYear = Tlc.GetTerrestrialBranch(lun60Year) - 1; int lunMonth = Tlc.GetMonth(DateTime.Now.AddYears(0)); int leapMonth = Tlc.GetLeapMonth(Tlc.GetYear(DateTime.Now.AddYears(0))); if (leapMonth > 0 && lunMonth >= leapMonth) { //lunMonth = lunMonth - 1; lunMonth -= 1; } int lunDay = Tlc.GetDayOfMonth(DateTime.Now.AddYears(0)); //Console.WriteLine("debug" + TeanGean[TeanGeanYear] + DeGe[DeGeYear] + CAnimal[DeGeYear]); Console.WriteLine("驗算網頁 : http://tlcheng.twbbs.org/Model/Online/LunarCalendar/Default.aspx"); Console.WriteLine("參考資料 : http://anita-lo.blogspot.com/2008/03/net_20.html"); Console.WriteLine("查表法 : http://chmis.cca.gov.tw/chmp/blog/a571024/myBlogArticleAction.do?method=doListArticleByPk&articleId=3213"); Console.WriteLine("參考資料 : http://www.fushantang.com/1003/c1007.html"); //return String.Format("農曆:{0}年{1}月{2}日", TeanGean[TeanGeanYear] & DeGe[DeGeYear], lunMonth, lunDay); //return String.Format("農曆:{0}年{1}月{2}日 今年的生肖: {3}",Tlc.GetYear(DateTime.Now).ToString(), lunMonth, lunDay, CAnimal[DeGeYear].ToString()); return(String.Format("{0}", CAnimal[DeGeYear].ToString())); }
/// <summary> /// 取得天干地支年月日 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static string GetTaiwanHeaEarDate(this DateTime dt) { string teanGean = "甲乙丙丁戊己庚辛壬癸"; string deGe = "子丑寅卯辰巳午未申酉戌亥"; TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar(); int lun60Year = tlc.GetSexagenaryYear(DateTime.Now.AddYears(0)); int TeanGeanYear = tlc.GetCelestialStem(lun60Year) - 1; int DeGeYear = tlc.GetTerrestrialBranch(lun60Year) - 1; int lunMonth = tlc.GetMonth(DateTime.Now.AddYears(0)); int leapMonth = tlc.GetLeapMonth(tlc.GetYear(DateTime.Now.AddYears(0))); if (leapMonth > 0 && lunMonth >= leapMonth) { lunMonth -= 1; } int lunDay = tlc.GetDayOfMonth(DateTime.Now.AddYears(0)); return(String.Format("{0}年{1}月{2}日", teanGean[TeanGeanYear].ToString() + deGe[DeGeYear].ToString(), lunMonth, lunDay)); }
/// <summary> /// 取得農曆的日期 /// </summary> /// <param name="dt">日期</param> /// <returns></returns> public static string GetLunisolarDate(DateTime dt) { string lunisolarDate = ""; TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar(); if (tlc.IsLeapYear(tlc.GetYear(dt)) && tlc.GetMonth(dt) >= tlc.GetLeapMonth(tlc.GetYear(dt))) { lunisolarDate = FormatLunisolarYear(tlc.GetYear(dt)) + FormatLunisolarMonth(tlc.GetMonth(dt) - 1) + FormatLunisolarDay(tlc.GetDayOfMonth(dt)); } else { lunisolarDate = FormatLunisolarYear(tlc.GetYear(dt)) + FormatLunisolarMonth(tlc.GetMonth(dt)) + FormatLunisolarDay(tlc.GetDayOfMonth(dt)); } //return "農曆 " + lunisolarDate; return(lunisolarDate); }
/// <summary> /// 排序樣板 By 日期順序,先來的要先排序在前面被檢測 /// </summary> /// <param name="festivalTemplates"></param> /// <param name="beforeDay">前幾天(節慶)</param> /// <returns></returns> private IList <TemplateVO> SortTemplateByDate(IList <TemplateVO> festivalTemplates, int beforeDay) { IList <TemplateVO> result = new List <TemplateVO>(); if (festivalTemplates != null && festivalTemplates.Count > 0) { foreach (TemplateVO templateVO in festivalTemplates) { if (templateVO.Name.Equals("聖誕節") || templateVO.Name.Equals("西洋情人節")) { //此兩個節日用西元算 DateTime today = DateTime.Today; int year = DateTime.Today.Year; int month = int.Parse(templateVO.StartDate.Substring(0, 2)); int day = int.Parse(templateVO.StartDate.Substring(2, 2)); DateTime endDate = new DateTime(DateTime.Today.Year, month, day); DateTime startDate = endDate.AddDays(((-1) * beforeDay)); templateVO.BeginDate = startDate; templateVO.LastDate = endDate; } else { //此其他的節日用農曆算 //先從農曆轉成西元 TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar(); DateTime today = DateTime.Today; int year = tlc.GetYear(today); int month = int.Parse(templateVO.StartDate.Substring(0, 2)); int day = int.Parse(templateVO.StartDate.Substring(2, 2)); int era = tlc.GetEra(today); //春節的話因為還沒到這個年,所以要加1個月 if (templateVO.Name.Equals("春節")) { year = tlc.GetYear(DateTime.Today.AddMonths(1)); era = tlc.GetEra(DateTime.Today.AddMonths(1)); } //判斷今年是否為閏年,閏年的話抓出正確的日期 if (tlc.IsLeapYear(year, era)) { //閏月月份 int leapMonth = tlc.GetLeapMonth(year, era); //閏月天數 //int monthDays = tlc.GetDaysInMonth(year, leapMonth, era); //閏月有13個月,例如leapMonth=5,代表閏四月,則農曆5月開始要多加1個月 if (month >= leapMonth) { month += 1; } } DateTime endDate = tlc.ToDateTime(year, month, day, 0, 0, 0, 0, era); DateTime startDate = endDate.AddDays(((-1) * beforeDay)); templateVO.BeginDate = startDate; templateVO.LastDate = endDate; } } result = festivalTemplates.OrderBy(p => p.BeginDate).ToList(); } return(result); }
protected void Calendar01_SelectionChanged(object sender, EventArgs e) { using DataTable dtDate = new CglData().GetDateData(Calendar01.SelectedDate.Date); DataRow drDAte = dtDate.NewRow(); // 找農曆 TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar(); int leapMouth = tlc.GetLeapMonth(tlc.GetYear(Calendar01.SelectedDate.Date)); int LuniMouth = tlc.GetMonth(Calendar01.SelectedDate.Date); if (leapMouth > 0) { if (LuniMouth == leapMouth) { LuniMouth = leapMouth - 1; } else if (LuniMouth > leapMouth) { LuniMouth -= 1; } } drDAte["lngDateSN"] = string.Format(InvariantCulture, "{0}{1:d2}{2:d2}", Calendar01.SelectedDate.Year, Calendar01.SelectedDate.Month, Calendar01.SelectedDate.Day); drDAte["lngCDateSN"] = string.Format(InvariantCulture, "{0}{1:d2}{2:d2}", tlc.GetYear(Calendar01.SelectedDate.Date), LuniMouth, tlc.GetDayOfMonth(Calendar01.SelectedDate.Date)); //找紫微 StuPurple stuTemp = new StuPurple { StrWYear = Calendar01.SelectedDate.Year.ToString(InvariantCulture), StrWMonth = Calendar01.SelectedDate.Month.ToString("d2", InvariantCulture), StrWDay = Calendar01.SelectedDate.Day.ToString("d2", InvariantCulture), StrWHour = "YY11", IntGender = CglPurple.Gender.MalePlus }; stuTemp.Init(); foreach (KeyValuePair <string, string> KeyPair in stuTemp.DicUpdateData) { if (KeyPair.Key != "lngDateSN") { if (KeyPair.Value.Length >= 4) { drDAte[KeyPair.Key] = KeyPair.Value.Substring(0, 4); } else { drDAte[KeyPair.Key] = KeyPair.Value; } } } // dtDate.Rows.Add(drDAte); GridView gvDate = new GalaxyApp().CreatGridView("gvDate", " gltabel ", dtDate, true, true); gvDate.DataBind(); pnlCalendar.Controls.Add(gvDate); // 一些使用的範例程式 //StringBuilder stringBuilder = new StringBuilder(0,8000); //for (int i = 2007; i <= 2050; i++) //{ // CNDate cd1 = new CNDate(new DateTime(i, Calendar01.SelectedDate.Date.Month, Calendar01.SelectedDate.Date.Day)); // stringBuilder.AppendLine("計算 " + i + "年隔年農曆過年的國曆日期:" + cd1.GetNextLunarNewYearDate().ToLongDateString() + "<br/>"); //} //CNDate cd = new CNDate(Calendar01.SelectedDate); //stringBuilder.AppendLine("今年農曆天數:" + cd.LunarYearDays(2007).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 01 月農曆天數:" + cd.LunarMonthDays(2007, 1).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 02 月農曆天數:" + cd.LunarMonthDays(2007, 2).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 03 月農曆天數:" + cd.LunarMonthDays(2007, 3).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 04 月農曆天數:" + cd.LunarMonthDays(2007, 4).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 05 月農曆天數:" + cd.LunarMonthDays(2007, 5).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 06 月農曆天數:" + cd.LunarMonthDays(2007, 6).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 07 月農曆天數:" + cd.LunarMonthDays(2007, 7).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 08 月農曆天數:" + cd.LunarMonthDays(2007, 8).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 09 月農曆天數:" + cd.LunarMonthDays(2007, 9).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 10 月農曆天數:" + cd.LunarMonthDays(2007, 10).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 11 月農曆天數:" + cd.LunarMonthDays(2007, 11).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今年 12 月農曆天數:" + cd.LunarMonthDays(2007, 12).ToString(InvariantCulture) + "<br/>"); //stringBuilder.AppendLine("今天的農曆日期:" + cd.GetLunarHolDay() + "<br/>"); //stringBuilder.AppendLine("今年的農曆潤月月份:" + cd.GetLeapMonth(2007) + "<br/>"); //stringBuilder.AppendLine("計算國曆當天對應的節氣:" + cd.LGetLunarHolDay() + "<br/>"); //stringBuilder.AppendLine("計算今年農曆過年的國曆日期:" + cd.GetLunarNewYearDate().ToLongDateString() + "<br/>"); //lblCC.Text = stringBuilder.ToString(); }
/// <summary> /// 取得目前要使用的樣板 /// </summary> /// <param name="beforeDay">前幾天(節慶)</param> /// <returns>目前要使用的樣板</returns> public TemplateVO GetCurrentTemplate(int beforeDay) { //先抓節日的 (要存農曆日還是西元要再確認,存西元必須每年度去調整) IList <TemplateVO> festivalTemplates = GetTemplateList(TemplateVO.Type.Festival); if (festivalTemplates != null && festivalTemplates.Count > 0) { foreach (TemplateVO templateVO in festivalTemplates) { if (templateVO.Name.Equals("聖誕節") || templateVO.Name.Equals("西洋情人節")) { //此兩個節日用西元算 DateTime today = DateTime.Today; int year = DateTime.Today.Year; int month = int.Parse(templateVO.StartDate.Substring(0, 2)); int day = int.Parse(templateVO.StartDate.Substring(2, 2)); DateTime endDate = new DateTime(DateTime.Today.Year, month, day); DateTime startDate = endDate.AddDays(((-1) * beforeDay)); if (today >= startDate && today <= endDate) { return(templateVO); } } else { //此其他的節日用農曆算 //先從農曆轉成西元 TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar(); DateTime today = DateTime.Today; int year = tlc.GetYear(today); int month = int.Parse(templateVO.StartDate.Substring(0, 2)); int day = int.Parse(templateVO.StartDate.Substring(2, 2)); int era = tlc.GetEra(today); //判斷今年是否為閏年,閏年的話抓出正確的日期 if (tlc.IsLeapYear(year, era)) { //閏月月份 int leapMonth = tlc.GetLeapMonth(year, era); //閏月天數 //int monthDays = tlc.GetDaysInMonth(year, leapMonth, era); //閏月有13個月,例如leapMonth=5,代表閏四月,則農曆5月開始要多加1個月 if (month >= leapMonth) { month += 1; } } DateTime endDate = tlc.ToDateTime(year, month, day, 0, 0, 0, 0, era); DateTime startDate = endDate.AddDays(((-1) * beforeDay)); if (today >= startDate && today <= endDate) { return(templateVO); } } } } //季節 IList <TemplateVO> seasonTemplates = GetTemplateList(TemplateVO.Type.Season); if (seasonTemplates != null && seasonTemplates.Count > 0) { DateTime date = DateTime.Today.AddDays(beforeDay); string showDate = string.Format("{0}{1}", date.Month.ToString().PadLeft(2, '0'), date.Day.ToString().PadLeft(2, '0')); int count = 0; foreach (TemplateVO templateVO in seasonTemplates) { count += 1; if (count < seasonTemplates.Count) { if (int.Parse(showDate) >= int.Parse(templateVO.StartDate) && int.Parse(showDate) <= int.Parse(templateVO.EndDate)) { return(templateVO); } } else { //最後一季的算法不一樣,可能有跨年(起始日期>結束時 則表示有跨年) if (int.Parse(templateVO.StartDate) > int.Parse(templateVO.EndDate)) { if (int.Parse(showDate) >= int.Parse(templateVO.StartDate) && int.Parse(showDate) <= int.Parse("1231")) { return(templateVO); } if (int.Parse(showDate) >= int.Parse("0101") && int.Parse(showDate) <= int.Parse(templateVO.EndDate)) { return(templateVO); } } else { if (int.Parse(showDate) >= int.Parse(templateVO.StartDate) && int.Parse(showDate) <= int.Parse(templateVO.EndDate)) { return(templateVO); } } } } } //都沒有的話回傳季節的第一個樣板 return(seasonTemplates[0]); }
public string GetLunarDate(DateTime mdate, string mtype) { String_Func sfc = new String_Func(); TaiwanLunisolarCalendar tlc = new TaiwanLunisolarCalendar(); string ldate = ""; int LunarYear = 0; // 農曆年 int LunarMonth = 0; // 月份 int LunarDay = 0; // 日期 int LunarHour = 0; // 時 int LunarMin = 0; // 分 int LunarSec = 0; // 秒 int LeapMonth = 0; // 潤月 LunarYear = tlc.GetSexagenaryYear(mdate); // 取得西元年 #region 農曆年 if (mtype.Contains("y")) { ldate = GetHeavenlyStem(tlc.GetCelestialStem(LunarYear)); // 年 - 天干 ldate += GetEarthlyBranch(tlc.GetTerrestrialBranch(LunarYear)) + "年"; // 年 - 地支 } #endregion #region 農曆月 if (mtype.Contains("M")) { LunarMonth = tlc.GetMonth(mdate); // 取得月份 LeapMonth = tlc.GetLeapMonth(tlc.GetYear(mdate)); // 取得潤月 if (LeapMonth > 0) { // 當年有潤月,月份會出現13個月,在潤月之後的月分要減一。 if (LeapMonth == LunarMonth) { ldate += "閏" + GetChMonth(LeapMonth - 1) + "月"; } else if (LunarMonth > LeapMonth) { ldate += GetChMonth(LunarMonth - 1) + "月"; } else ldate += GetChMonth(LunarMonth) + "月"; } else ldate += GetChMonth(LunarMonth) + "月"; } #endregion #region 農曆日 if (mtype.Contains("d")) { LunarDay = tlc.GetDayOfMonth(mdate); ldate += GetChDay(LunarDay) + "日"; } #endregion #region 農曆時 (子、丑...) if (mtype.Contains("H")) { LunarHour = tlc.GetHour(mdate); ldate += GetChHour(LunarHour) + "時"; } #endregion #region 中文數字時 (五、十一...) if (mtype.Contains("h")) { LunarHour = tlc.GetHour(mdate); ldate += GetChNHour(LunarHour) + "時"; } #endregion #region 農曆分 if (mtype.Contains("m")) { LunarMin = tlc.GetMinute(mdate); ldate += sfc.GetChNumber((ulong)LunarMin).Replace("一十", "十") + "分"; } #endregion #region 農曆秒 if (mtype.Contains("s")) { LunarSec = tlc.GetSecond(mdate); if (LunarSec == 0) { ldate += "整"; } else { ldate += sfc.GetChNumber((ulong)LunarSec).Replace("一十", "十") + "秒"; } } #endregion return ldate; }