除日期字段外,所有数据都成立
string strDate = ds.Tables[0].Rows[0]["H_DT"].ToString();
抛出异常说:
Specified time is not supported in this calendar. It should be between
04/30/1900 00:00:00 (Gregorian date) and 11/16/2077 23:59:59
(Gregorian date),inclusive.
我试着写这段代码
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-sa"); System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-sa");
把文化改成阿拉伯语,但没有任何运气.
更新
以下是变量快速监视的屏幕截图
解决方法
DateTime.ToString
method起
The
ToString()
method returns the string representation of the date
and time in the calendar used by the current culture. If the value of
the currentDateTime
instance is earlier than
Calendar.MinSupportedDateTime
or later than
Calendar.MaxSupportedDateTime
,the method throws an
ArgumentOutOfRangeException
.
您的ar-sa文化的默认日历是UmAlQuraCalendar
calender.
var culture = CultureInfo.GetCultureInfo("ar-sa"); Console.WriteLine(culture.Calendar); // prints UmAlQuraCalendar
从UmAlQuraCalendar.MinSupportedDateTime
Property起
The earliest date and time supported by the UmAlQuraCalendar class,
which is equivalent to the first moment of April 30,1900 C.E. in the
Gregorian calendar.
由于你的DateTime是1,1,198,因此抛出ArgumentOutOfRangeException太正常了.
您可以解决您的问题,在DateTime.ToString()方法中提供参数IFormatProvider
,默认情况下为GregorianCalendar
.例如,您可以使用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 application but I faced the same error,please clarify
me,thanks
默认情况下,DateTime属于公历.从DateTime
structure;
Each
DateTime
member implicitly uses the Gregorian calendar to perform
its operation,with the exception of constructors that specify a
calendar,and methods with a parameter derived fromIFormatProvider
,
such asSystem.Globalization.DateTimeFormatInfo
,that implicitly
specifies a calendar.
这意味着您的ds.Tables [0] .Rows [0] [“H_DT”]日期时间默认为格里高利日历.但是,由于您使用的是没有任何参数的.ToString()方法,因此您的方法使用的是您自己在web.config中编写的CurrentCulture.并且该文化默认具有UmAlQuraCalendar日历.由于您的日期时间超出了此日历范围,因此您的代码会引发异常.
请记住,在Gregorian日历中,您有一个1318年的日期时间,而UmAlQuraCalendar日历中的年份为1318年.
举个例子;
var date = new DateTime(1318,1); Console.WriteLine(date.ToString(new CultureInfo("ar-sa")));
抛出ArgumentOutOfRangeException异常,因为它与你的情况完全相同.这是一个日历时间,在格里高利日历中是1318年,但是在这个日期时间的UmAlQuraCalendar日历上没有任何代表,因为在UmAlQuraCalendar日历中,年份从格里高利日历中的1900年开始.
看看UmAlQuraCalendar
calender implemented如何;
//////////////////////////////////////////////////////////////////////////// // // Notes about UmAlQuraCalendar // //////////////////////////////////////////////////////////////////////////// /* ** Calendar support range: ** Calendar Minimum Maximum ** ========== ========== ========== ** Gregorian 1900/04/30 2077/11/17 ** UmAlQura 1318/01/01 1500/12/30 */