c# – 在asp.net中接收阿拉伯语日期时间错误

前端之家收集整理的这篇文章主要介绍了c# – 在asp.net中接收阿拉伯语日期时间错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用ADO断开模式通过填充数据集ds从数据库获取数据.
除日期字段外,所有数据都成立
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 @H_403_29@ToString() method returns the string representation of the date
and time in the calendar used by the current culture. If the value of
the current @H_403_29@DateTime instance is earlier than
@H_403_29@Calendar.MinSupportedDateTime or later than
@H_403_29@Calendar.MaxSupportedDateTime,the method throws an
@H_403_29@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 @H_403_29@ar-sa to be
global in all application but I faced the same error,please clarify
me,thanks

默认情况下,DateTime属于公历.从DateTime structure;

Each @H_403_29@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 from @H_403_29@IFormatProvider,
such as @H_403_29@System.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
 */
原文链接:https://www.f2er.com/csharp/239021.html

猜你在找的C#相关文章