Convert.ToDateTime throws exception when language is set to Arabic(SaudiArebia)

Jignesh Desai 101 Reputation points
2021-02-17T09:14:03.323+00:00

Hi,

I am changing UI language based on user selection as below

Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(ChangeLanguageTo);
Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(ChangeLanguageTo);

When language is changed To Arabic following line of code thows exception "String was not recognized as a valid DateTime".

newDataTableRow["Start"] = pEventItem.Start.DateTime.HasValue ? Convert.ToDateTime(pEventItem.Start.DateTime) : Convert.ToDateTime(pEventItem.Start.Date);

Here PEventItem is of type Google.Apis.Calendar.v3.Data.Event; Here I am trying to read existing Google calendar event item.

I tried to set 2nd parameter of the Convert.ToDateTime function in an hope that it might convert date correctly... eg.

CultureInfo enUsCulture = new CultureInfo("en-us");
newDataTableRow["Start"] = pEventItem.Start.DateTime.HasValue ? Convert.ToDateTime(pEventItem.Start.DateTime, enUsCulture) : Convert.ToDateTime(pEventItem.Start.Date, enUsCulture);

Well Now it does not throw exception, but the returned date is incorrect. value retuned is {21/10/40 12:00:00 ص}

while The actual date pEventItem.Start.Date is "2019-06-24" (as seen from debug window)

What could be the issue ? Can anyone guide please .

Regards

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,448 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
775 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Jignesh Desai 101 Reputation points
    2021-02-18T11:00:50.92+00:00

    Hi @DaisyTian-MSFT

    Here i was experimenting with a code.

    private void btn1_Click(object sender, RoutedEventArgs e)
        {
            string strDate = "2021-01-01T14:30:00+05:30";
            StringBuilder strList = new StringBuilder();
    
            strList.Append(string.Format("Original String Date : {0}{1}", strDate, Environment.NewLine));
    
            DateTime d1 = Convert.ToDateTime(strDate);
            strList.Append(string.Format("Thread Culture: {0} -> DateTime d1 = Convert.ToDateTime(strDate);  returns : {1}{2}", Thread.CurrentThread.CurrentCulture.Name,  d1.ToString(), Environment.NewLine));
    
            DateTime d2 = Convert.ToDateTime(strDate,  CultureInfo.GetCultureInfo("ar-SA"));
            strList.Append(string.Format("Thread Culture: {0} ->      DateTime d2 = Convert.ToDateTime(strDate,  CultureInfo.GetCultureInfo('ar-SA'));  returns : {1}{2}", Thread.CurrentThread.CurrentCulture.Name, d2.ToString(), Environment.NewLine));
    
            DateTime d3 = Convert.ToDateTime(strDate, CultureInfo.GetCultureInfo("ja-JP"));
            strList.Append(string.Format("Thread Culture: {0} ->      DateTime d3 = Convert.ToDateTime(strDate,  CultureInfo.GetCultureInfo('ja-JP'));  returns : {1}{2}", Thread.CurrentThread.CurrentCulture.Name, d3.ToString(), Environment.NewLine));
    
            var ss = d1.ToString(CultureInfo.GetCultureInfo("ar-SA"));
            strList.Append(string.Format("Thread Culture: {0} ->     var ss = d1.ToString(CultureInfo.GetCultureInfo('ar-SA'));     returns : {1}{2}", Thread.CurrentThread.CurrentCulture.Name, ss, Environment.NewLine));
    
            DateTime d4 = Convert.ToDateTime(d1, CultureInfo.GetCultureInfo("ar-SA"));
            strList.Append(string.Format("Thread Culture: {0} ->     DateTime d4 = Convert.ToDateTime(d1, CultureInfo.GetCultureInfo('ar-SA'));     returns : {1}{2}", Thread.CurrentThread.CurrentCulture.Name, d4.ToString(), Environment.NewLine));
    
    
            var culture = new CultureInfo("ar-SA") { NumberFormat = { DigitSubstitution = DigitShapes.NativeNational } };
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
            strList.Append(string.Format("Thread Culture: {0} ->   DateTime.Now.ToString();      returns : {1}{2}", Thread.CurrentThread.CurrentCulture.Name, DateTime.Now.ToString(), Environment.NewLine));
    
            tbResult.Text = strList.ToString();
        }
    

    and it produce the following output.

    1. Original String Date : 2021-01-01T14:30:00+05:30
    2. Thread Culture: en-US -> DateTime d1 = Convert.ToDateTime(strDate); returns : 1/1/2021 2:30:00 PM
    3. Thread Culture: en-US -> DateTime d2 = Convert.ToDateTime(strDate, CultureInfo.GetCultureInfo('ar-SA')); returns : 1/1/2021 2:30:00 PM
    4. Thread Culture: en-US -> DateTime d3 = Convert.ToDateTime(strDate, CultureInfo.GetCultureInfo('ja-JP')); returns : 1/1/2021 2:30:00 PM
    5. Thread Culture: en-US -> var ss = d1.ToString(CultureInfo.GetCultureInfo('ar-SA')); returns : 17/05/42 02:30:00 م
    6. Thread Culture: en-US -> DateTime d4 = Convert.ToDateTime(d1, CultureInfo.GetCultureInfo('ar-SA')); returns : 1/1/2021 2:30:00 PM
    7. Thread Culture: ar-SA -> DateTime.Now.ToString(); returns : 06/07/42 04:18:44 م

    At Point 2, 5 and 6 , I was expecting it would return 17/05/42 02:30:00 م but strangely it returns 1/1/2021 2:30:00 PM ignoring given culture ar-SA.

    Why so ?

    Regards

    1 person found this answer helpful.