From DateTime.ToString method
The ToString() method returns a string representation of the date and time in the calendar used by the current culture. If the value of the current DateTime instance is earlier than Calendar.MinSupportedDateTime or later than Calendar.MaxSupportedDateTime , the method throws an ArgumentOutOfRangeException .
The ar-sa culture calendaring is the UmAlQuraCalendar calender .
var culture = CultureInfo.GetCultureInfo("ar-sa"); Console.WriteLine(culture.Calendar);
And from the UmAlQuraCalendar.MinSupportedDateTime Property
The earliest date and time supported by the UmAlQuraCalendar class, which is equivalent to the first moment on April 30, 1900 CE in the Gregorian calendar .
Since your DateTime is 1, 1, 1398 , it is too normal to throw an ArgumentOutOfRangeException .
You can solve your problem by specifying the IFormatProvider parameter in your DateTime.ToString() method, which is GregorianCalendar by default. For example, you can use InvariantCulture .
string strDate = ds.Tables[0].Rows[0]["H_DT"].ToString(CultureInfo.InvariantCulture);
I wrote globalization configuration in web config as ar-sa to be global in all applications, but I encountered the same error, please clarify me, thanks
A DateTime by default belongs to the Gregorian calendar. From a DateTime structure ;
Each DateTime member implicitly uses the Gregorian calendar to do its job, with the exception of constructors that define a calendar and methods with a parameter derived from IFormatProvider , such as System.Globalization.DateTimeFormatInfo , which implicitly defines a calendar.
This means that your datetime ds.Tables[0].Rows[0]["H_DT"] is the default Gregorian calendar. But since you are using the .ToString() method without any parameters, your method uses your CurrentCulture , which ar-sa , since you wrote it in your web.config. And this culture has a UmAlQuraCalendar calendar by default. Since your datetime is out of range in this calendar, your code throws an exception.
Remember that you have a DateTime with 1318 as a year in the Gregorian calendar, not 1318 as a year in the UmAlQuraCalendar .
As an example:
var date = new DateTime(1318, 1, 1); Console.WriteLine(date.ToString(new CultureInfo("ar-sa")));
throws ArgumentOutOfRangeException exception, because this is exactly the same as your case. This is DateTime, which is 1318 in the Gregorian calendar, but this datetime does not exist in the UmAlQuraCalendar calendar, because in the UmAlQuraCalendar years begin from 1900 in the Gregorian calendar.
See how the UmAlQuraCalendar calendar is implemented ;
//////////////////////////////////////////////////////////////////////////// // // Notes about UmAlQuraCalendar // //////////////////////////////////////////////////////////////////////////// /* ** Calendar support range: ** Calendar Minimum Maximum ** ========== ========== ========== ** Gregorian 1900/04/30 2077/11/17 ** UmAlQura 1318/01/01 1500/12/30 */