private void cldLunarDayCalendar_SelectedDateChanged(object sender, EventArgs e) { currentSolarDate = new SolarDate(cldLunarDayCalendar.SelectedDate); currentLunarDate = currentSolarDate.ToLunarDate(timeZone); DayCalendar_UpdateValue(); if (currentSolarDate == SolarDate.MinValue) { btnDayCalendar_PreviousDate.Enabled = false; } else { btnDayCalendar_PreviousDate.Enabled = true; } if (currentSolarDate == SolarDate.MaxValue) { btnDayCalendar_NextDate.Enabled = false; } else { btnDayCalendar_NextDate.Enabled = true; } }
private void cmbDayCalendar_Lunar_Month_SelectedIndexChanged(object sender, EventArgs e) { try { lblDayCalendar_Error.Visible = false; btnDayCalendar_PreviousDate.Enabled = true; btnDayCalendar_NextDate.Enabled = true; currentLunarMonth = int.Parse(cmbDayCalendar_Lunar_Month.Text.Substring(0, 2)); if (cmbDayCalendar_Lunar_Month.Text.Length > 2) { currentLunarIsLeapMonth = true; } DayCalendar_ModifySelectedDate_Lunar(); LunarDate currentLunarDate = new LunarDate(currentLunarDay, currentLunarMonth, currentLunarIsLeapMonth, currentLunarYear, timeZone); SolarDate currentSolarDate = currentLunarDate.ToSolarDate(); currentSolarYear = currentSolarDate.Year; currentSolarMonth = currentSolarDate.Month; currentSolarDay = currentSolarDate.Day; cldLunarDayCalendar.SelectedDate = new DateTime(currentSolarYear, currentSolarMonth, currentSolarDay); } catch (Exception exc) { lblDayCalendar_Error.Text = "Lỗi: " + exc.Message; lblDayCalendar_Error.Visible = true; btnDayCalendar_PreviousDate.Enabled = false; btnDayCalendar_NextDate.Enabled = false; } }
private void DayCalendar_ModifySelectedDate_Lunar() { //Month ArrayList months = LunarDate.GetMonths(currentLunarYear, timeZone); if (currentLunarIsLeapMonth) { bool flag = false; foreach (string month in months) { if (month.Length > 2 && month.Substring(0, 2) == currentLunarMonth.ToString("00")) { flag = true; break; } } if (!flag) { currentLunarIsLeapMonth = false; } } //Day int n = LunarDate.GetNumberOfDaysInMonth(currentLunarMonth, currentLunarIsLeapMonth, currentLunarYear, timeZone); if (currentLunarDay > n) { currentLunarDay = n; } }
private void cldLunarDayCalendar_SelectedDateChanged(object sender, EventArgs e) { currentSolarYear = cldLunarDayCalendar.SelectedDate.Year; currentSolarMonth = cldLunarDayCalendar.SelectedDate.Month; currentSolarDay = cldLunarDayCalendar.SelectedDate.Day; SolarDate solarDate = new SolarDate(currentSolarDay, currentSolarMonth, currentSolarYear); LunarDate lunarDate = solarDate.ToLunarDate(timeZone); currentLunarYear = lunarDate.Year; currentLunarMonth = lunarDate.Month; currentLunarIsLeapMonth = lunarDate.IsLeapMonth; currentLunarDay = lunarDate.Day; DayCalendar_UpdateValue(); if (solarDate == SolarDate.MinValue) { btnDayCalendar_PreviousDate.Enabled = false; } else { btnDayCalendar_PreviousDate.Enabled = true; } if (solarDate == SolarDate.MaxValue) { btnDayCalendar_NextDate.Enabled = false; } else { btnDayCalendar_NextDate.Enabled = true; } }
private void cmbDayCalendar_Lunar_Day_SelectedIndexChanged(object sender, EventArgs e) { if (cmbDayCalendar_Lunar_Day.SelectedIndex == -1) { return; } try { lblDayCalendar_Error.Visible = false; btnDayCalendar_PreviousDate.Enabled = true; btnDayCalendar_NextDate.Enabled = true; int currentLunarDay = int.Parse(cmbDayCalendar_Lunar_Day.Text); currentLunarDate = new LunarDate(currentLunarDay, currentLunarDate.Month, currentLunarDate.IsLeapMonth, currentLunarDate.Year, timeZone); currentSolarDate = currentLunarDate.ToSolarDate(); cldLunarDayCalendar.SelectedDate = new DateTime(currentSolarDate.Year, currentSolarDate.Month, currentSolarDate.Day); } catch (Exception exc) { lblDayCalendar_Error.Text = "Lỗi: " + exc.Message; lblDayCalendar_Error.Visible = true; btnDayCalendar_PreviousDate.Enabled = false; btnDayCalendar_NextDate.Enabled = false; } }
private void cldLunarMonthCalendar_SelectedMonthChanged(object sender, EventArgs e) { currentMonth = cldLunarMonthCalendar.SelectedMonth.Month; currentYear = cldLunarMonthCalendar.SelectedMonth.Year; txtMonthCalendar_Year.Text = cldLunarMonthCalendar.SelectedMonth.Year.ToString("0000"); cmbMonthCalendar_Month.Text = cldLunarMonthCalendar.SelectedMonth.Month.ToString("00"); if (currentSolarYear == 1 && currentMonth == 1) { btnMonthCalendar_PreviousMonth.Enabled = false; } else { btnMonthCalendar_PreviousMonth.Enabled = true; } if (currentYear == 9999 && currentMonth == 12) { btnMonthCalendar_NextMonth.Enabled = false; } else { btnMonthCalendar_NextMonth.Enabled = true; } SolarDate sd = new SolarDate(cldLunarMonthCalendar.SelectedMonth); LunarDate ld = sd.ToLunarDate(timeZone); }
public static ArrayList GetMonths(int year, int timeZone) { ArrayList months = new ArrayList(); LunarDate lunarDate1 = new LunarDate(1, 1, false, year, timeZone); LunarDate newMoonDay = lunarDate1; SolarDate testDate_Solar; LunarDate testDate_Lunar; do { string month = newMoonDay.Month.ToString("00"); if (newMoonDay.IsLeapMonth) { month += " nhuận"; } months.Add(month); if (year == 9999 && month == "12") { break; } testDate_Solar = new SolarDate(newMoonDay.JulianDayNumber + 32); testDate_Lunar = testDate_Solar.ToLunarDate(timeZone); newMoonDay = new LunarDate(1, testDate_Lunar.Month, testDate_Lunar.isLeapMonth, year, timeZone); }while (testDate_Lunar.Year == year); return(months); }
private void txtDayCalendar_Lunar_Year_Validated(object sender, EventArgs e) { try { lblDayCalendar_Error.Visible = false; btnDayCalendar_PreviousDate.Enabled = true; btnDayCalendar_NextDate.Enabled = true; currentLunarYear = int.Parse(txtDayCalendar_Lunar_Year.Text); DayCalendar_ModifySelectedDate_Lunar(); LunarDate currentLunarDate = new LunarDate(currentLunarDay, currentLunarMonth, currentLunarIsLeapMonth, currentLunarYear, timeZone); SolarDate currentSolarDate = currentLunarDate.ToSolarDate(); currentSolarYear = currentSolarDate.Year; currentSolarMonth = currentSolarDate.Month; currentSolarDay = currentSolarDate.Day; cldLunarDayCalendar.SelectedDate = new DateTime(currentSolarYear, currentSolarMonth, currentSolarDay); } catch (Exception exc) { lblDayCalendar_Error.Text = "Lỗi: " + exc.Message; lblDayCalendar_Error.Visible = true; btnDayCalendar_PreviousDate.Enabled = false; btnDayCalendar_NextDate.Enabled = false; } }
private void DayCalendar_ModifySelectedDate_Lunar(int lunarMonth, bool lunarIsLeapMonth, int lunarYear) { //Check mininum value if (lunarYear == 0) { if (currentLunarDate.Month < 11) { currentLunarDate = new LunarDate(19, 11, false, 0, timeZone); } else if (currentLunarDate.Month == 11) { if (currentLunarDate.Day < 19) { currentLunarDate = new LunarDate(19, 11, false, 0, timeZone); } else { currentLunarDate = new LunarDate(currentLunarDate.Day, 11, false, 0, timeZone); } } } //Month ArrayList months = LunarDate.GetMonths(lunarYear, timeZone); if (lunarIsLeapMonth) { bool flag = false; foreach (string month in months) { if (month.Length > 2 && month.Substring(0, 2) == lunarMonth.ToString("00")) { flag = true; break; } } if (!flag) { currentLunarDate = new LunarDate(currentLunarDate.Day, lunarMonth, false, lunarYear, timeZone); } } //Day int n = LunarDate.GetNumberOfDaysInMonth(lunarMonth, lunarIsLeapMonth, lunarYear, timeZone); if (currentLunarDate.Day > n) { currentLunarDate = new LunarDate(n, lunarMonth, lunarIsLeapMonth, lunarYear, timeZone); } else { currentLunarDate = new LunarDate(currentLunarDate.Day, lunarMonth, lunarIsLeapMonth, lunarYear, timeZone); } currentSolarDate = currentLunarDate.ToSolarDate(); }
public static SolarDate ConvertLunarDateToSolarDate(LunarDate LunarDate) { if ((LunarDate.Year < 0) || (LunarDate.Year == 0 && LunarDate.Month < 11) || (LunarDate.Year == 0 && LunarDate.Month == 11 && LunarDate.Day < 19)) { throw new Exception("Vượt quá giới hạn chuyển đổi!"); } if ((LunarDate.Year > 9999) || (LunarDate.Year == 9999 && LunarDate.Month == 12 && LunarDate.Day > 2)) { throw new Exception("Vượt quá giới hạn chuyển đổi!"); } long a11 = 0; long b11 = 0; if (LunarDate.Month < 11) { a11 = GetLunarMonth11(LunarDate.Year - 1, LunarDate.TimeZone); b11 = GetLunarMonth11(LunarDate.Year, LunarDate.TimeZone); } else { a11 = GetLunarMonth11(LunarDate.Year, LunarDate.TimeZone); b11 = GetLunarMonth11(LunarDate.Year + 1, LunarDate.TimeZone); } long k = (long)Math.Floor(0.5 + (a11 - 2415021.07699869) / 29.530588853); int off = LunarDate.Month - 11; if (off < 0) { off = off + 12; } if (b11 - a11 > 365) { int leapOff = GetLeapMonthOffset(a11, LunarDate.TimeZone); int leapMonth = leapOff - 2; if (leapMonth < 0) { leapMonth = leapMonth + 12; } if (LunarDate.IsLeapMonth && LunarDate.Month != leapMonth) { throw new Exception("Không thể thực hiện chuyển đổi!"); } if (LunarDate.IsLeapMonth || off >= leapOff) { off = off + 1; } } long monthStart = GetNewMoonDay(k + off, LunarDate.TimeZone); return(new SolarDate(monthStart + LunarDate.Day - 1)); }
public Main() { InitializeComponent(); currentSolarDate = new SolarDate(DateTime.Today); currentLunarDate = currentSolarDate.ToLunarDate(timeZone); currentMonth = DateTime.Today.Month; currentYear = DateTime.Today.Year; DayCalendar_FillValue(); MonthCalendar_FillValue(); cldLunarDayCalendar.SelectedDate = DateTime.Today; cldLunarMonthCalendar.SelectedMonth = DateTime.Today; }
public static int GetNumberOfDaysInMonth(int month, bool isLeapMonth, int year, int timeZone) { if (year == 9999 && month == 12) { return(29); } LunarDate newMoonDay_Lunar = new LunarDate(1, month, isLeapMonth, year, timeZone); SolarDate newMoonDay_Solar = newMoonDay_Lunar.ToSolarDate(); SolarDate testDate_Solar = new SolarDate(newMoonDay_Solar.JulianDayNumber + 32); LunarDate testDate_Lunar = testDate_Solar.ToLunarDate(timeZone); LunarDate nextNewMoonDay_Lunar = new LunarDate(1, testDate_Lunar.Month, testDate_Lunar.isLeapMonth, testDate_Lunar.Year, timeZone); SolarDate nextNewMoonDay_Solar = nextNewMoonDay_Lunar.ToSolarDate(); return((int)(nextNewMoonDay_Solar.JulianDayNumber - newMoonDay_Solar.JulianDayNumber)); }
private void DayCalendar_FillValue() { //Solar Date //Year txtDayCalendar_Solar_Year.Text = currentSolarYear.ToString("0000"); //Month cmbDayCalendar_Solar_Month.Text = currentSolarMonth.ToString("00"); //Day cmbDayCalendar_Solar_Day.Items.Clear(); int n = SolarDate.GetNumberOfDaysInMonth(currentSolarMonth, currentSolarYear); for (int i = 1; i <= n; i++) { cmbDayCalendar_Solar_Day.Items.Add((i).ToString("00")); } cmbDayCalendar_Solar_Day.Text = currentSolarDay.ToString("00"); //Lunar Date //Year txtDayCalendar_Lunar_Year.Text = currentLunarYear.ToString("0000"); //Month cmbDayCalendar_Lunar_Month.Items.Clear(); foreach (string month in LunarDate.GetMonths(currentLunarYear, timeZone)) { cmbDayCalendar_Lunar_Month.Items.Add(month); } string str = currentLunarMonth.ToString("00"); if (currentLunarIsLeapMonth) { str += " nhuận"; } cmbDayCalendar_Lunar_Month.Text = str; //Day cmbDayCalendar_Lunar_Day.Items.Clear(); int m = LunarDate.GetNumberOfDaysInMonth(currentLunarMonth, currentLunarIsLeapMonth, currentLunarYear, timeZone); for (int i = 1; i <= m; i++) { cmbDayCalendar_Lunar_Day.Items.Add(i.ToString("00")); } cmbDayCalendar_Lunar_Day.Text = currentLunarDay.ToString("00"); }
public static long JulianDayNumberFromLunarDate(LunarDate LunarDate) { long a11 = 0; long b11 = 0; if (LunarDate.Month < 11) { a11 = GetLunarMonth11(LunarDate.Year - 1, LunarDate.TimeZone); b11 = GetLunarMonth11(LunarDate.Year, LunarDate.TimeZone); } else { a11 = GetLunarMonth11(LunarDate.Year, LunarDate.TimeZone); b11 = GetLunarMonth11(LunarDate.Year + 1, LunarDate.TimeZone); } long k = (long)Math.Floor(0.5 + (a11 - 2415021.07699869) / 29.530588853); int off = LunarDate.Month - 11; if (off < 0) { off = off + 12; } if (b11 - a11 > 365) { int leapOff = GetLeapMonthOffset(a11, LunarDate.TimeZone); int leapMonth = leapOff - 2; if (leapMonth < 0) { leapMonth = leapMonth + 12; } if (LunarDate.IsLeapMonth && LunarDate.Month != leapMonth) { throw new Exception("Không thể thực hiện chuyển đổi!"); } if (LunarDate.IsLeapMonth || off >= leapOff) { off = off + 1; } } long monthStart = GetNewMoonDay(k + off, LunarDate.TimeZone); return(monthStart + LunarDate.Day - 1); }
//Khởi tạo public frmMain() { InitializeComponent(); currentSolarYear = DateTime.Today.Year; currentSolarMonth = DateTime.Today.Month; currentSolarDay = DateTime.Today.Day; SolarDate today_Solar = new SolarDate(currentSolarDay, currentSolarMonth, currentSolarYear); LunarDate today_Lunar = today_Solar.ToLunarDate(timeZone); currentLunarYear = today_Lunar.Year; currentLunarMonth = today_Lunar.Month; currentLunarIsLeapMonth = today_Lunar.IsLeapMonth; currentLunarDay = today_Lunar.Day; DayCalendar_FillValue(); MonthCalendar_FillValue(); cldLunarDayCalendar.SelectedDate = DateTime.Today; cldLunarMonthCalendar.SelectedMonth = DateTime.Today; }
private void DayCalendar_UpdateValue() //Modify the Selected Date and Update Value { //Solar Date //Year txtDayCalendar_Solar_Year.Text = currentSolarYear.ToString("0000"); //Month cmbDayCalendar_Solar_Month.Text = currentSolarMonth.ToString("00"); //Day int n = SolarDate.GetNumberOfDaysInMonth(currentSolarMonth, currentSolarYear); if (n != cmbDayCalendar_Solar_Day.Items.Count) { if (n > cmbDayCalendar_Solar_Day.Items.Count) { for (int i = cmbDayCalendar_Solar_Day.Items.Count + 1; i <= n; i++) { cmbDayCalendar_Solar_Day.Items.Add(i.ToString("00")); } } else { if (int.Parse(cmbDayCalendar_Solar_Day.Text) > n) { cmbDayCalendar_Solar_Day.Text = currentSolarDay.ToString("00"); } for (int i = cmbDayCalendar_Solar_Day.Items.Count - 1; i >= n; i--) { cmbDayCalendar_Solar_Day.Items.RemoveAt(i); } } } cmbDayCalendar_Solar_Day.Text = currentSolarDay.ToString("00"); //Lunar Date //Year txtDayCalendar_Lunar_Year.Text = currentLunarYear.ToString("0000"); //Month ArrayList months = months = LunarDate.GetMonths(currentLunarYear, timeZone); for (int i = cmbDayCalendar_Lunar_Month.Items.Count - 1; i >= 0; i--) { string month = (string)cmbDayCalendar_Lunar_Month.Items[i]; if (month.Length > 2) { bool flag = false; foreach (string month1 in months) { if (month1 == month) { flag = true; break; } } if (!flag) { cmbDayCalendar_Lunar_Month.Items.RemoveAt(i); } } } foreach (string month in months) { if (month.Length > 2) { bool flag = false; foreach (string month1 in cmbDayCalendar_Lunar_Month.Items) { if (month1 == month) { flag = true; break; } } if (!flag) { cmbDayCalendar_Lunar_Month.Items.Add(month); } } } string str = currentLunarMonth.ToString("00"); if (currentLunarIsLeapMonth) { str += " nhuận"; } cmbDayCalendar_Lunar_Month.Text = str; //Day int m = LunarDate.GetNumberOfDaysInMonth(currentLunarMonth, currentLunarIsLeapMonth, currentSolarYear, timeZone); if (m != cmbDayCalendar_Lunar_Day.Items.Count) { if (m > cmbDayCalendar_Lunar_Day.Items.Count) { for (int i = cmbDayCalendar_Lunar_Day.Items.Count + 1; i <= m; i++) { cmbDayCalendar_Lunar_Day.Items.Add((i).ToString("00")); } } else { if (int.Parse(cmbDayCalendar_Lunar_Day.Text) > m) { cmbDayCalendar_Lunar_Day.Text = currentLunarDay.ToString("00"); } for (int i = cmbDayCalendar_Lunar_Day.Items.Count - 1; i >= m; i--) { cmbDayCalendar_Lunar_Day.Items.RemoveAt(i); } } } cmbDayCalendar_Lunar_Day.Text = currentLunarDay.ToString("00"); }
private void DayCalendar_UpdateValue() { //Solar Date //Year txtDayCalendar_Solar_Year.Text = currentSolarDate.Year.ToString("0000"); //Month cmbDayCalendar_Solar_Month.Text = currentSolarDate.Month.ToString("00"); //Day cmbDayCalendar_Solar_Day.Items.Clear(); int n = SolarDate.GetNumberOfDaysInMonth(currentSolarDate.Month, currentSolarDate.Year); for (int i = 1; i <= n; i++) { cmbDayCalendar_Solar_Day.Items.Add(i.ToString("00")); } cmbDayCalendar_Solar_Day.Text = currentSolarDate.Day.ToString("00"); //Lunar Date //Year txtDayCalendar_Lunar_Year.Text = currentLunarDate.Year.ToString("0000"); //Month if (currentLunarDate.Year > 0) { cmbDayCalendar_Lunar_Month.Items.Clear(); ArrayList months = months = LunarDate.GetMonths(currentLunarDate.Year, timeZone); foreach (string month in months) { cmbDayCalendar_Lunar_Month.Items.Add(month); } string str = currentLunarDate.Month.ToString("00"); if (currentLunarDate.IsLeapMonth) { str += " nhuận"; } cmbDayCalendar_Lunar_Month.Text = str; } else if (currentLunarDate.Year == 0) { cmbDayCalendar_Lunar_Month.Items.Clear(); cmbDayCalendar_Lunar_Month.Items.Add("11"); cmbDayCalendar_Lunar_Month.Items.Add("12"); cmbDayCalendar_Lunar_Month.Text = currentLunarDate.Month.ToString("00"); } //Day if (currentLunarDate.Year > 0 || (currentLunarDate.Year == 0 && currentLunarDate.Month > 11)) { cmbDayCalendar_Lunar_Day.Items.Clear(); int m = LunarDate.GetNumberOfDaysInMonth(currentLunarDate.Month, currentLunarDate.IsLeapMonth, currentLunarDate.Year, timeZone); for (int i = 1; i <= m; i++) { cmbDayCalendar_Lunar_Day.Items.Add((i).ToString("00")); } cmbDayCalendar_Lunar_Day.Text = currentLunarDate.Day.ToString("00"); } else if (currentLunarDate.Year == 0 && currentLunarDate.Month == 11) { cmbDayCalendar_Lunar_Day.Items.Clear(); for (int i = 19; i <= 30; i++) { cmbDayCalendar_Lunar_Day.Items.Add(i.ToString()); } cmbDayCalendar_Lunar_Day.Text = currentLunarDate.Day.ToString("00"); } }
public static SolarDate ConvertLunarDateToSolarDate(LunarDate lunarDate) { long julianDayNumber = JulianDayNumberFromLunarDate(lunarDate); return(new SolarDate(julianDayNumber)); }