public void PosTest2() { System.Globalization.Calendar tc = new TaiwanCalendar(); Random rand = new Random(-55); int year = 2000; int month = 2; int day; DateTime dt; for (day = 1; day < 30; day++) { dt = new DateTime(year, month, day); Assert.Equal(tc.GetDayOfMonth(dt), day); } }
public void PosTest1() { System.Globalization.Calendar tc = new TaiwanCalendar(); Random rand = new Random(-55); int year = rand.Next(tc.MinSupportedDateTime.Year, tc.MaxSupportedDateTime.Year - 1911); int month = rand.Next(1, 12); int day; if (tc.IsLeapYear(year)) { day = rand.Next(1, 30); } else { day = rand.Next(1, 29); } DateTime dt = tc.ToDateTime(year, month, day, 0, 0, 0, 0); int actualDays = dt.Day; Assert.Equal(tc.GetDayOfMonth(dt), actualDays); }
public void PosTest4() { System.Globalization.Calendar tc = new TaiwanCalendar(); DateTime dt = tc.MinSupportedDateTime; Assert.Equal(1, tc.GetDayOfMonth(dt)); }
/// <summary> /// 取得現在民國日期時間{yyy.MM.dd-HH} /// </summary> /// <returns></returns> private string GetTaiwanCalendarDateTime() { System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar(); DateTime d = DateTime.Now; int hour = d.Hour; if (d.Minute != 0 || d.Second != 0) //判斷非整點無條件進位 { hour++; //直接+1 23點多顯示24 } string result = string.Format("{0:000}.{1:00}.{2:00}-{3:00}", tc.GetYear(d), tc.GetMonth(d), tc.GetDayOfMonth(d), hour); return(result); }
//取德 public void ToChineseDate() { DateTime dNow = DateTime.Now; System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar(); DateTime d = new DateTime(2010, 8, 18, 1, 0, 0); string dateString2 = string.Format("{0:000}.{1:00}.{2:00}-{3:00}", tc.GetYear(d), tc.GetMonth(d), tc.GetDayOfMonth(d), d.Hour + 1); string dateString1 = string.Format("民國{0:000}年{1:00}月{2:00}日", tc.GetYear(d), tc.GetMonth(d), tc.GetDayOfMonth(d)); string dateString3 = string.Format("-{0:00}", d.Hour + 1); Console.WriteLine(dateString1); Console.WriteLine(dateString2); Console.WriteLine(dateString3); }
private void button2_Click(object sender, EventArgs e) { label3.Text = ""; /*test*/ System.Globalization.CultureInfo tc = new System.Globalization.CultureInfo("zh-TW"); //tc.DateTimeFormat.Calendar = new System.Globalization.TaiwanLunisolarCalendar(); DateTime dt2 = DateTime.Now; //label5.Text = dt2.ToString(tc); ; /*test2*/ CultureInfo m_ciTaiwan = new CultureInfo("zh-TW"); m_ciTaiwan.DateTimeFormat.Calendar = m_ciTaiwan.OptionalCalendars[2]; string strDate = DateTime.Now.Date.ToString("yyyyMMdd", m_ciTaiwan); // label4.Text=strDate;//output:1000512 DateTime dtNow = DateTime.ParseExact(strDate.PadLeft(8, '0'), "yyyyMMdd", m_ciTaiwan); /*test3*/ /**/ string dd = dateTimePicker1.Text; dd = dd.Replace("年", ",").Replace("月", ",").Replace("日", ","); string[] ddd = { " ", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十", "三十一" }; string[] ss = { "", "秦廣王", "楚江王", "宋帝王", "伍官王", "閻羅王", "變成王", "泰山王", "平等王", "都市王", "輪轉王" }; string[] arrday = { "", "首七", "二七", "三七", "四七", "五七", "六七", "滿七", "百日", "對年" }; string[] arr = dd.Split(",".ToCharArray()); Dictionary<int,int> lsCalendar = new Dictionary<int,int>() { {2020,4 } } int a = (int)decimal.Parse(arr[0]); int b = (int)decimal.Parse(arr[1]); int c = (int)decimal.Parse(arr[2]); DateTime dtObj = new DateTime(2018, 6, 11); //以 月份 日, 年 的格式輸出 string outputDate = dtObj.ToString("MMMM dd, yyyy", new CultureInfo("zh-tw")); //label7.Text = outputDate; DateTime dt = new DateTime(a, b, c); Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-tw"); System.Globalization.TaiwanCalendar TC = new System.Globalization.TaiwanCalendar(); System.Globalization.TaiwanLunisolarCalendar TA = new System.Globalization.TaiwanLunisolarCalendar(); label1.Text = string.Format("民國:{0}/{1}/{2}", TC.GetYear(dt), TC.GetMonth(dt), TC.GetDayOfMonth(dt)) ; // label2.Text = string.Format("農歷:{0}<br>", TA.AddDays(dt, 99)); //label4.Text = string.Format("民國:{0}/{1}/{2}",TA.GetYear(), ) int count = 4; for (int i = 1; i <= 7; i++) { //做七 int d = 7 * i; int dn; // label3.Text += arrday[i] + " " + dt.AddDays(d - 1).ToString("MMMM dd, yyyy", new CultureInfo("zh-tw")) + Environment.NewLine; if (i == 2) { dn = int.Parse(dt.AddDays((d/2) - 1).ToString("dd")); label3.Text += "地方風俗 " +" " + dt.AddDays((d/2) - 2).ToString("MM 月dd", new CultureInfo("zh-tw")) + "日" + Environment.NewLine; } dn = int.Parse(dt.AddDays(d - 1).ToString("dd")); label3.Text += ss[i].PadRight(4,' ') + " " + arrday[i] + " " + dt.AddDays(d - 1).ToString("MM 月dd", new CultureInfo("zh-tw")) +"日"+Environment.NewLine; count += 1; } //百日 label6.Text = dt.AddDays(99).ToString("yyyy年MM月dd日 ", new CultureInfo("zh-tw")); //對年 string result = string.Empty; System.Globalization.TaiwanLunisolarCalendar tls = new System.Globalization.TaiwanLunisolarCalendar(); DateTime begin = tls.AddDays(DateTime.Now, 0); Boolean leap = tls.IsLeapYear(TA.GetYear(dt)); DateTime dtt = tls.ToDateTime(TA.GetYear(dt), TA.GetMonth(dt), TA.GetDayOfMonth(dt), 0, 0, 0, 0); if (leap) { //label5.Text = "適逢"+ TA.GetYear(dt.AddYears(1) )+ "年閏月,因而農曆月份須提前一個月"; System.Diagnostics.Debug.WriteLine(a); if (TA.GetMonth(dtt) > lsCalendar[(int)a]) { dtt = tls.AddMonths(dtt,-1); } } label2.Text = string.Format("農曆:{0}/{1}/{2}", TA.GetYear(dtt), TA.GetMonth(dtt), TA.GetDayOfMonth(dtt)); dtt = tls.AddYears(dtt, 1); TimeSpan tss = dtt - begin; int day = tls.GetDayOfMonth(dtt); int month = tls.GetMonth(dtt); int year = tls.GetYear(dtt); label4.Text = string.Format("國歷{3}\n農曆{0}年{1}月{2}日", year, month, day, DateTime.Now.Add(tss).ToString("yyyy/MM/dd")); }
public string DateView(string gValue, string oType) { //00:2017-12-12 //01: //02:106.12.12 //03:2016年04月13日 //04:106-12-12 string rValue = ""; try { switch (oType) { case "00": rValue = Convert.ToDateTime(gValue).ToString("yyyy-MM-dd"); break; case "01": System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar(); DateTime d; if (DateTime.TryParse(gValue, out d)) { d = Convert.ToDateTime(gValue); rValue = String.Format("{0}年{1}月{2}日", tc.GetYear(d), tc.GetMonth(d), tc.GetDayOfMonth(d)); } break; case "02": System.Globalization.TaiwanCalendar tc2 = new System.Globalization.TaiwanCalendar(); DateTime d2; if (DateTime.TryParse(gValue, out d2)) { d2 = Convert.ToDateTime(gValue); rValue = String.Format("{0}.{1:00}.{2:00}", tc2.GetYear(d2), tc2.GetMonth(d2), tc2.GetDayOfMonth(d2)); } break; case "03": rValue = Convert.ToDateTime(gValue).ToString("yyyy年MM月dd日"); break; case "04": System.Globalization.TaiwanCalendar tc4 = new System.Globalization.TaiwanCalendar(); DateTime d4; if (DateTime.TryParse(gValue, out d2)) { d2 = Convert.ToDateTime(gValue); rValue = String.Format("{0}-{1:00}-{2:00}", tc4.GetYear(d2), tc4.GetMonth(d2), tc4.GetDayOfMonth(d2)); } break; } } catch { rValue = gValue; } if (gValue != "") { if (gValue.Substring(0, 4) == "1900") { rValue = ""; } } return(rValue); }