Year Month and Day parameters describe an un-representable DateTime

Last Reply on May 20, 2013 06:57 AM By RkHirpara

Posted on May 20, 2013 01:19 AM

HI

 

 I use below code to showing date in Persian in my site

 

 

 

        DateTime miladi = DateTime.Now;
        System.Globalization.PersianCalendar shamsi = new System.Globalization.PersianCalendar();
        DateTime shamsidate = new DateTime(shamsi.GetYear(miladi), shamsi.GetMonth(miladi), shamsi.GetDayOfMonth(miladi));





        Lbldate.Text = (shamsidate.ToString("yyyy/MM/dd", System.Globalization.CultureInfo.InvariantCulture));

but below erro happen

Server Error in '/behtop website' Application.


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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.

Source Error: 

Line 43:         DateTime miladi = DateTime.Now;
Line 44:         System.Globalization.PersianCalendar shamsi = new System.Globalization.PersianCalendar();
Line 45: DateTime shamsidate = new DateTime(shamsi.GetYear(miladi), shamsi.GetMonth(miladi), shamsi.GetDayOfMonth(miladi)); Line 46: 
Line 47: 

it worked correctly before but suddenly it make an error Why?

Best Regards

Thanks alot

Posted on May 20, 2013 06:57 AM

Better use the following way....

DateTime miladi = DateTime.Now;
                System.Globalization.PersianCalendar shamsi = new System.Globalization.PersianCalendar();
                Lbldate.Text = string.Format("{0}/{1}/{2}", shamsi.GetYear(miladi), shamsi.GetMonth(miladi), shamsi.GetDayOfMonth(miladi));