Asp.net arabic datetime error

I use ADO Disabled mode to get data from the database by populating the ds dataset; all data is correct except for the date recorded

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

it throws an exception:

The specified time is not supported on this calendar. It should be between 04/30/1900 00:00:00 (Gregorian date) and 11/16/2077 23:59:59 (Gregorian date), inclusive.

I tried to write this code

 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 no luck please someone can help me.

Update:

below is a screenshot of a quick variable view

enter image description here

+6
source share
4 answers

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); // prints UmAlQuraCalendar 

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 */ 
+7
source

Try the following:

 var ci = CultureInfo.CreateSpecificCulture("ar-SA"); string strDate = ds.Tables[0].Rows[0]["H_DT"].ToString(ci); 

You will convey the desired culture when you convert the date to a string.

0
source

---> cause of this problem

hijri date in DateTime Object in C # is between 01/01/1318 and 30/12/1500.

in gregorian date between 1900 and 2077

therefore, if we open the sql database and get the date and convert to hijri date, we note that this is not between the two hijri dates above

to make sure that you can convert it by opening any converter website online


----> solutions

you have 2 solutions to solve this problem

-solidation 1

you can update date values ​​in sql

set it to a valid date (between 1900 and 2077)

OR

-solution 2

in your code, before you get the date from the data string, you must set CurrentCulture to " ru ", and when you finish the code, you must set the language to " ar ".

check the code below

 bool isLanguageChanged = false; if (Thread.CurrentThread.CurrentCulture.Name == "ar") { isLanguageChanged = true; Thread.CurrentThread.CurrentCulture = new CultureInfo("en"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("en"); } string dateStr = dataTable.Rows[i]["DateColumn"].ToString(); if (isLanguageChanged) { Thread.CurrentThread.CurrentCulture = new CultureInfo("ar"); Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar"); } 
0
source

Use InvariantCulture when converting dates to strings in restricted cultures, for example:

 using System.Globalization; string str = someDate.ToString(CultureInfo.InvariantCulture); 
0
source

Source: https://habr.com/ru/post/978088/


All Articles