The parameters Year, Month, and Day describe an unimaginable DateTime in the Persian calendar.

I used the following code to represent the Persian date today ... the site I was working on was online two months ago and it did a great job, but today I got the following error:

The Year, Month, and Day parameters describe non-representable DateTimes.

and I have no idea why this happened ... is there a way to fix this?

protected void lbnChangeDate_Click(object sender, EventArgs e) { PersianCalendar p = new PersianCalendar(); DateTime date = DateTime.Now; int year = p.GetYear(date); int month = p.GetMonth(date); int day = p.GetDayOfMonth(date); DateTime d1 = new DateTime(year, month, day); MultiView6.SetActiveView(View12); txtDate.Text = DateTime.Parse(d1.ToString()).ToString("yyyy/MM/dd"); } 
+2
source share
3 answers

DateTime do not remember how they were created. So, for example, d1 does not remember that at some point in the past you used an instance of PersianCalendar to get 3 PersianCalendar , and then use them in your constructor. This constructor (implicitly) suggested that you go through the years, months, and days from the Gregorian calendar.

Then, in addition, DateTime.Parse does not know that for a long time you turned to PersianCalendar and did unulocal with it.

If you want to show the Persian calendar date as a string, just use it like:

 protected void lbnChangeDate_Click(object sender, EventArgs e) { PersianCalendar p = new PersianCalendar(); DateTime date = DateTime.Now; txtDate.Text = string.Format("{0:0000}/{1:00}/{2:00}", p.GetYear(date), p.GetMonth(date), p.GetDayOfMonth(date)); } 
+7
source

the answer works fine ... now I want to change the Persian date specified in txtDate.Text to a regular date in order to save it in the database. For example, I want to change 1393/05/05 to 2014/05/26 then save it to the database ... I used the following code, however, I encountered the same problem discussed in the first post ...

  date = Convert.ToDateTime(txtDate.Text); string change = date.ToString("yyyy/MM/dd"); int day1 = Convert.ToInt32(change.Substring(8, 2)); int mon1 = Convert.ToInt32(change.Substring(5, 2)); int year1 = Convert.ToInt32(change.Substring(0, 4)); PersianCalendar pc = new PersianCalendar(); change = (pc.ToDateTime(year1, mon1, day1, 0, 0, 0, 0).ToString("yyyy/MM/dd").Substring(0, 10)); date = Convert.ToDateTime(change); 

Is there a way to fix this too?

+1
source

An exception error occurs in this piece of code:

 System.DateTime.DateToTicks(Int32 year, Int32 month, Int32 day) 

What caused:

 [__DynamicallyInvokable] public DateTime(int year, int month, int day) { this.dateData = (ulong) DateTime.DateToTicks(year, month, day); } 

When executing this code, it looks like this:

 private static long DateToTicks(int year, int month, int day) { if (year >= 1 && year <= 9999 && (month >= 1 && month <= 12)) { int[] numArray = DateTime.IsLeapYear(year) ? DateTime.DaysToMonth366 : DateTime.DaysToMonth365; if (day >= 1 && day <= numArray[month] - numArray[month - 1]) { int num = year - 1; return (long) (num * 365 + num / 4 - num / 100 + num / 400 + numArray[month - 1] + day - 1) * 864000000000L; } } throw new ArgumentOutOfRangeException((string) null, Environment.GetResourceString("ArgumentOutOfRange_BadYearMonthDay")); } 

The only way the method throws an exception is when the year is less than 1 or more than 9999, and the month is not between 1 and 12. Therefore, I suggest that you make sure that you pass valid dates.

+1
source

All Articles