在ASP.NET中收到阿拉伯语日期时间错误 [英] Receiving an Arabic datetime error in asp.net

查看:189
本文介绍了在ASP.NET中收到阿拉伯语日期时间错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ADO断开连接模式通过填充数据集ds从数据库获取数据。
除日期字段外的所有数据都变为真

I use ADO disconnected mode to get data from database by filling dataset ds. All data come true except the date field

string strDate = ds.Tables[0].Rows[0]["H_DT"].ToString();

它抛出异常说:


此日历不支持指定的时间。它应该在
04/30/1900 00:00:00(格雷戈里日期)和11/16/2077 23:59:59
(格雷戈里日期)之间。

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

将文化更改为阿拉伯语,但没有运气。

to change the culture to Arabic but without any luck.

以下是快速查看变量的屏幕截图

The following is screenshot of quick watch for the variable

推荐答案

DateTime.ToString 方法


ToString()方法返回当前区域性使用的日历中日期
和时间的字符串表示形式。如果
的值是当前 DateTime 实例早于
Calendar.MinSupportedDateTime 或更高版本比
Calendar.MaxSupportedDateTime ,该方法将抛出
ArgumentOutOfRangeException

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 current DateTime instance is earlier than Calendar.MinSupportedDateTime or later than Calendar.MaxSupportedDateTime, the method throws an ArgumentOutOfRangeException.

您的 ar-sa 文化的默认日历为 UmAlQuraCalendar 日历

Your ar-sa culture's default calender is UmAlQuraCalendar calender.

var culture = CultureInfo.GetCultureInfo("ar-sa");
Console.WriteLine(culture.Calendar); // prints UmAlQuraCalendar

并从 UmAlQuraCalendar.MinSupportedDateTime 属性


UmAlQuraCalendar类支持的最早日期和时间,
相当于 4月的第一时刻公元1900年30月30日

由于您的 DateTime 1,1,1398 ,这太正常了,不能抛出 ArgumentOutOfRangeException

Since your DateTime is 1, 1, 1398, it is too normal to throws ArgumentOutOfRangeException.

您可以解决问题,以提供参数您的 DateTime.ToString()中的 IFormatProvider 具有 GregorianCalendar 默认情况下。您可以使用 <$例如,c $ c> InvariantCulture

You can solve your problem to provide parameter an IFormatProvider in your DateTime.ToString() method which has GregorianCalendar by default. You can use InvariantCulture for example.

string strDate = ds.Tables[0].Rows[0]["H_DT"].ToString(CultureInfo.InvariantCulture);




我在Web配置中将全球化配置写为 ar-sa 在所有应用程序中都是
,但我遇到了相同的错误,请向我说明
,谢谢

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

A DateTime 默认属于公历 。来自 DateTime 结构;

A DateTime belongs on Gregorian calendar by default. From DateTime structure;


每个 DateTime 成员隐式使用公历执行
的操作,但指定
日历的构造函数以及具有从 IFormatProvider
(例如 System.Globalization.DateTimeFormatInfo ,即
隐式指定一个日历。

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 from IFormatProvider, such as System.Globalization.DateTimeFormatInfo, that implicitly specifies a calendar.

这意味着默认情况下,您的 ds.Tables [0] .Rows [0] [ H_DT] 日期时间是公历日历。但是由于您使用的是 .ToString()方法而没有任何参数,因此您的方法使用的是 CurrentCulture ,即 ar-sa ,因为您在web.config中编写了它。默认情况下,该文化具有 UmAlQuraCalendar 日历。由于该日历中的日期时间超出范围,因此代码将引发异常。

That means your ds.Tables[0].Rows[0]["H_DT"] datetime is Gregorian calender by default. But since you using .ToString() method without any parameter, your method uses your CurrentCulture which is ar-sa since you wrote it in your web.config. And that culture has UmAlQuraCalendar calender by default. Since your datetime out of range in this calender, your code throws exception.

请记住,您有一个 DateTime 1318 作为公历的一年,不是 1318 作为<$ c的一年$ c> UmAlQuraCalendar 日历。

Remember, you have a DateTime with 1318 as a year in Gregorian calender, not 1318 as a year in UmAlQuraCalendar calender.

例如;

var date = new DateTime(1318, 1, 1);
Console.WriteLine(date.ToString(new CultureInfo("ar-sa")));

引发 ArgumentOutOfRangeException 异常,因为它正是同样的情况。这是一个DateTime,在公历中是 1318 年,但是在 UmAlQuraCalendar 日历中没有表示形式日期时间,因为在 UmAlQuraCalendar 日历中,年份从 1900 在格里高利日历中开始。

throws ArgumentOutOfRangeException exception because it is exactly the same case of yours. This is a DateTime which is a 1318 year in a Gregorian calender, but there is no representation on UmAlQuraCalendar calender of this datetime because in UmAlQuraCalendar calender, years start with 1900 in Gregorian calender.

看看 UmAlQuraCalendar 日历已实施;

Take a look at how 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
 */

这篇关于在ASP.NET中收到阿拉伯语日期时间错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆