Receiving an Arabic datetime error in asp.net

后端 未结 4 1584
名媛妹妹
名媛妹妹 2020-12-11 03:56

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][\         


        
相关标签:
4条回答
  • 2020-12-11 04:19

    Use the InvariantCulture when converting dates to strings under limited cultures, just like this:

    using System.Globalization;
    string str = someDate.ToString(CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2020-12-11 04:28

    --->cause of this issue

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

    in gregorian date is between years 1900 and 2077

    so if we open the sql database and get the date and convert to hijri date we note that is not between the 2 hijri date above

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


    ---->solutions

    you have 2 solution to resolve this issue

    -solusion 1

    you can update the values of date in sql

    set it to real date (between 1900 and 2077)

    OR

    -solution 2

    in your code before you get the date from datatable row you should to set the CurrentCulture to "en" and when you finish the code you should 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 讨论(0)
  • 2020-12-11 04:29

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

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

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

    And from 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.

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

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

    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 belongs on Gregorian calendar by default. From 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 from IFormatProvider, such as System.Globalization.DateTimeFormatInfo, that implicitly specifies a calendar.

    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.

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

    As an example;

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

    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.

    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
     */
    
    0 讨论(0)
  • 2020-12-11 04:34

    Try this:

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

    You will pass you desired culture when you are converting date to string.

    0 讨论(0)
提交回复
热议问题