Year, Month, and Day parameters describe an un-representable DateTime in Persian calender

18,547

Solution 1

DateTimes don't remember how they were created. So, e.g. d1 doesn't remember that, at some point in the past, you used a PersianCalendar instance to obtain 3 ints and then use those in its constructor. That constructor (implicitly) assumed that you were passing years, months and days from the Gregorian calendar.

Then, further, DateTime.Parse has no knowledge that long ago you accessed a PersianCalendar and did inappropriate things with it.

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

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));

}

Solution 2

The exception error occurs at this code snippet :

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

Which called in:

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

When step through that code it's 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 that method throws the exception is when the year is less than 1 or greater than 9999 and the month is not between 1 and 12. So I suggest you making sure you are passing valid dates.

Solution 3

the answer works great...now I want to change the Persian date which is given in the txtDate.Text back to regular date to save it in the database.for example I want to change 1393/05/05 to 2014/05/26 then save it to 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?

Share:
18,547
Sara Nikta Yousefi
Author by

Sara Nikta Yousefi

Updated on June 08, 2022

Comments

  • Sara Nikta Yousefi
    Sara Nikta Yousefi almost 2 years

    I used the following code in order to present today's Persian date... the site I worked on have been online since two month ago and it have worked fine but today I received the following error:

    Year, Month, and Day parameters describe an un-representable DateTime.

    and I have no idea why this happened...is there any 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");
    
    }
    
  • GrantByrne
    GrantByrne almost 9 years
    This would mean that DateTime.Min and DateTime.Max would fail validation.