private void Box_TextChanged(object sender, TextChangedEventArgs e) { try { PDate pDate = new PDate(Convert.ToInt32(yearBox.Text), Convert.ToInt32(monthBox.Text), Convert.ToInt32(dayBox.Text)); if (pDate.Gregorian == DateTime.MinValue) { yearBox.Background = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)); monthBox.Background = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)); dayBox.Background = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)); } else { yearBox.Background = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255)); monthBox.Background = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255)); dayBox.Background = new SolidColorBrush(Color.FromArgb(0, 255, 255, 255)); } if (this.ValueChagned != null) { this.ValueChagned(this, null); } } catch { yearBox.Background = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)); monthBox.Background = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)); dayBox.Background = new SolidColorBrush(Color.FromArgb(0, 255, 0, 0)); } }
public PDate(int year, int month, int day) { DateTime startDate = new DateTime(year + 621, 1, 1); PDate temp = new PDate(startDate); while (!(temp.Year == year && temp.Month == month && temp.Day == day)) { startDate = startDate.AddDays(1); temp = new PDate(startDate); if (temp.Year >= year && temp.Month > month) { startDate = DateTime.MinValue; break; } } date = startDate; }
private static PDate ToPersianTime(DateTime miladi) { var calendar = System.Globalization.CultureInfo.CurrentCulture.Calendar; var monthes = new List<int> { 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 }; var kabiseMonthes = new List<int> { 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 30 }; int dayOfShamsiYear; int day = 1; int month = 1; int year = 1; //for the year if (miladi.DayOfYear > 79) { year = miladi.Year - 621; } else { year = miladi.Year - 622; } if (calendar.GetDaysInMonth(miladi.Year - 1, 2) == 29/*kabise*/) { dayOfShamsiYear = (miladi.DayOfYear + 287) % 366; if (dayOfShamsiYear == 0) dayOfShamsiYear = 366; day = dayOfShamsiYear; for (int i = 0; i < 12; i++) { day -= kabiseMonthes[i]; if (day <= 0) { day += kabiseMonthes[i]; month = i + 1; break; } } } else { dayOfShamsiYear = (miladi.DayOfYear + 286) % 365; if (dayOfShamsiYear == 0) dayOfShamsiYear = 365; day = dayOfShamsiYear; for (int i = 0; i < 12; i++) { day -= monthes[i]; if (day <= 0) { day += monthes[i]; month = i + 1; break; } } } //!!! wont work properly for example in 30 and 31 ordibehesht var shamsi = new PDate { Year = year, Month = month, Day = day }; return shamsi; }