ايران ويج

نسخه‌ی کامل: کلاس تقویم فارسی بدون کلاس پرشین کلندر
شما در حال مشاهده‌ی نسخه‌ی متنی این صفحه می‌باشید. مشاهده‌ی نسخه‌ی کامل با قالب بندی مناسب.
با سلام یک کلاس کامل تقدیم به همه دوستان
کد:
public class HejriShamsiDate
   {
       // Fields
       protected int day;
       protected int month;
       protected int year;

       // Methods
       public int AddDays(int days)
       {
           int num = Shamsi2Miladi(this.ToInteger());
           DateTime time = new DateTime(num/0x2710, (num/100)%100, num%100);
           int num2 = Miladi2Shamsi(time.AddDays((double) days));
           if (this.year < 100)
           {
               this.Year = (num2/0x2710)%100;
           }
           else
           {
               this.Year = num2/0x2710;
           }
           this.Month = (num2/100)%100;
           this.Day = num2%100;
           return (((this.year*0x2710) + (this.month*100)) + this.day);
       }

       public static int AddDays(int date, int days)
       {
           HejriShamsiDate date2 = ExtractDate(date);
           date2.AddDays(days);
           date2.Validate();
           return date2.ToInteger();
       }

       public int AddMonths(int months)
       {
           if (!CheckShamsiDate((long) this.ToInteger()))
           {
               throw new Exception("The Date is not valid");
           }
           int nYear = this.ToInteger()/0x2710;
           int num2 = (this.ToInteger()/100)%100;
           nYear = months/12;
           num2 = months%12;
           nYear += this.ToInteger()/0x2710;
           if (months != 0)
           {
               if (months > 0)
               {
                   num2 += this.month;
                   if (num2 > 12)
                   {
                       nYear++;
                       num2 -= 12;
                   }
               }
               else
               {
                   num2 += this.month - 12;
                   if ((num2*-1) >= 12)
                   {
                       nYear--;
                       num2 *= -1;
                   }
                   else
                   {
                       num2 = 12 - (num2*-1);
                   }
               }
           }
           if ((!isKabiseh(nYear) && (num2 == 12)) && (this.day == 30))
           {
               this.Day = 0x1d;
           }
           if ((num2 > 6) && (this.day == 0x1f))
           {
               this.Day = 30;
           }
           this.Year = nYear;
           this.Month = num2;
           return this.ToInteger();
       }

       public static int AddMonths(int date, int months)
       {
           HejriShamsiDate date2 = ExtractDate(date);
           date2.AddMonths(months);
           date2.Validate();
           return date2.ToInteger();
       }

       public int AddYears(int years)
       {
           if (!CheckShamsiDate((long) this.ToInteger()))
           {
               throw new Exception("The Date is not valid");
           }
           int nYear = this.ToInteger()/0x2710;
           nYear += years;
           this.Year = nYear;
           if ((!isKabiseh(nYear) && (this.Month == 12)) && (this.Day == 30))
           {
               this.Day = 0x1d;
           }
           return this.ToInteger();
       }

       private static bool CheckShamsiDate(long shamsiDate)
       {
           int num3 = (int) (shamsiDate%100L);
           int num2 = (int) ((shamsiDate/100L)%100L);
           int nYear = (int) (shamsiDate/0x2710L);
           if ((shamsiDate < 0x65L) || (shamsiDate > 0x5f5bebeL))
           {
               return false;
           }
           if ((num2 < 7) && ((num3 > 0x1f) || (num3 < 1)))
           {
               return false;
           }
           if ((num2 > 6) && ((num3 > 30) || (num3 < 1)))
           {
               return false;
           }
           if ((!isKabiseh(nYear) && (num2 == 12)) && (num3 == 30))
           {
               return false;
           }
           return true;
       }

       public void Clear()
       {
           this.month = 0;
           this.day = 0;
           this.year = 0;
       }

       public DayOfWeek DayOfWeek()
       {
           int num = Shamsi2Miladi(this.ToInteger());
           DateTime time = new DateTime(num/0x2710, (num/100)%100, num%100);
           return time.DayOfWeek;
       }

       public int DayOfYear()
       {
           return DaysInYear((long) this.ToInteger());
       }

       private static int DaysInYear(long shamsiDate)
       {
           int num2 = (int) (shamsiDate%100L);
           int num3 = (int) ((shamsiDate/100L)%100L);
           if (!CheckShamsiDate(shamsiDate))
           {
               throw new Exception("The Date is not valid");
           }
           if (num3 < 7)
           {
               return (((num3 - 1)*0x1f) + num2);
           }
           return ((((num3 - 7)*30) + num2) + 0xba);
       }

       public static HejriShamsiDate ExtractDate(int date)
       {
           HejriShamsiDate date2 = new HejriShamsiDate();
           date2.Year = date/0x2710;
           date2.Month = (date%0x2710)/100;
           date2.Day = date%100;
           return date2;
       }

       public static HejriShamsiDate ExtractDate(string date)
       {
           return ExtractDate(Convert.ToInt32(date));
       }

       public static HejriShamsiDate ExtractDate(int date, bool checkDate)
       {
           HejriShamsiDate date2 = ExtractDate(date);
           if (checkDate)
           {
               date2.Validate();
           }
           return date2;
       }

       public static int ExtractDay(int date, bool checkDate)
       {
           HejriShamsiDate date2 = new HejriShamsiDate();
           date2.Year = date/0x2710;
           date2.Month = (date%0x2710)/100;
           date2.Day = date%100;
           if (checkDate)
           {
               date2.Validate();
           }
           return date2.Day;
       }

       public static int ExtractMonth(int date, bool checkDate)
       {
           HejriShamsiDate date2 = new HejriShamsiDate();
           date2.Year = date/0x2710;
           date2.Month = (date%0x2710)/100;
           date2.Day = date%100;
           if (checkDate)
           {
               date2.Validate();
           }
           return date2.Month;
       }

       public static int ExtractYear(int date)
       {
           HejriShamsiDate date2 = new HejriShamsiDate();
           date2.Year = date/0x2710;
           date2.Month = (date%0x2710)/100;
           date2.Day = date%100;
           return date2.Year;
       }

       public static int ExtractYear(int date, bool checkDate)
       {
           ExtractDate(date, true);
           return ExtractYear(date);
       }

       public static int ExtractYearMonth(int date)
       {
           HejriShamsiDate date2 = new HejriShamsiDate();
           date2.Year = date/0x2710;
           date2.Month = (date%0x2710)/100;
           date2.Day = date%100;
           return ((date2.Year*100) + date2.Month);
       }

       public static int ExtractYearMonth(int date, bool checkDate)
       {
           HejriShamsiDate date2 = new HejriShamsiDate();
           date2.Year = date/0x2710;
           date2.Month = (date%0x2710)/100;
           date2.Day = date%100;
           if (checkDate)
           {
               date2.Validate();
           }
           return ExtractYearMonth(date);
       }

       private static bool isIntercalary(int nYear)
       {
           return ((((nYear%4) == 0) && ((nYear%100) != 0)) || ((nYear%400) == 0));
       }

       public static bool isKabiseh(int nYear)
       {
           if (nYear < 100)
           {
               nYear = 0x514 + nYear;
           }
           int num = nYear%0x21;
           if ((((num != 1) && (num != 5)) && ((num != 9) && (num != 13))) &&
               (((num != 0x11) && (num != 0x16)) && ((num != 0x1a) && (num != 30))))
           {
               return false;
           }
           return true;
       }

       public static int Miladi2Shamsi(DateTime miladiDate)
       {
           int num3;
           int num4;
           int index = 0;
           int[] numArray = new int[] {0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 30, 30, 30, 30, 30, 0x1d};
           int num7 = 0x16d;
           int year = miladiDate.Year;
           int dayOfYear = miladiDate.DayOfYear;
           if (isIntercalary(year - 1))
           {
               num7 = 0x16e;
               numArray[11] = 30;
           }
           if (dayOfYear > 0x4f)
           {
               num3 = year - 0x26d;
               num4 = dayOfYear - 0x4f;
           }
           else
           {
               num3 = year - 0x26e;
               num4 = (num7 - 0x4f) + dayOfYear;
           }
           int num = num4;
           while (num > numArray[index])
           {
               num -= numArray[index];
               index++;
           }
           index++;
           return (((num3*0x2710) + (index*100)) + num);
       }

       public static int Miladi2Shamsi(int year, int month, int day)
       {
           DateTime miladiDate = new DateTime(year, month, day);
           return Miladi2Shamsi(miladiDate);
       }

       public string PersianDayOfWeek()
       {
           string[] strArray = new string[] {"يکشنبه", "دوشنبه", "سه شنبه", "چهارشنبه", "پنج شنبه", "جمعه", "شنبه"};
           int num = Shamsi2Miladi(this.ToInteger());
           DateTime time = new DateTime(num/0x2710, (num/100)%100, num%100);
           int dayOfWeek = (int) time.DayOfWeek;
           return strArray[dayOfWeek].ToString();
       }

       public static int Shamsi2Miladi(int shamsiDate)
       {
           if (!CheckShamsiDate((long) shamsiDate))
           {
               throw new Exception("The Date is not valid");
           }
           int num4 = shamsiDate/0x2710;
           if (num4 < 100)
           {
               num4 = 0x514 + num4;
           }
           int num3 = DaysInYear((long) shamsiDate);
           int year = num4 + 0x26d;
           DateTime time = new DateTime(year, 1, 1);
           int num2 = num3 + 0x4e;
           time = time.AddDays((double) num2);
           return (((time.Year*0x2710) + (time.Month*100)) + time.Day);
       }

       public static int Shamsi2Miladi(int year, int month, int day)
       {
           int shamsiDate = ((year*0x2710) + (month*100)) + day;
           return Shamsi2Miladi(shamsiDate);
       }

       public static int Today()
       {
           return Miladi2Shamsi(DateTime.Today);
       }

       public int ToInteger()
       {
           return (((this.year*0x2710) + (this.month*100)) + this.day);
       }

       public static int ToInteger(int year, int month, int day)
       {
           HejriShamsiDate date = new HejriShamsiDate();
           date.Year = year;
           date.month = month;
           date.day = day;
           date.Validate();
           return date.ToInteger();
       }

       public override string ToString()
       {
           return this.ToInteger().ToString();
       }

       public void Validate()
       {
           if (!CheckShamsiDate((long) (((this.year*0x2710) + (this.month*100)) + this.day)))
           {
               throw new Exception("Date = " + (((this.year*0x2710) + (this.month*100)) + this.day) + " is invalid",
                                   null);
           }
       }

       // Properties
       public int Day
       {
           get { return this.day; }
           set { this.day = value; }
       }

       public int Month
       {
           get { return this.month; }
           set { this.month = value; }
       }

       public int Year
       {
           get { return this.year; }
           set { this.year = value; }
       }
   }
دستتون درد نکنه

یه چندتا مثال هم بذارید ازش خیلی خوب میشه
با سلام خیلی ساده اس از روی اسم متدها هم چیز مشخصه ولی باز چشم
کد:
label1.Text = HejriShamsiDate.Today().ToString();