C# How to convert date into en-US when thread culture is ar-SA

Joined
Feb 18, 2021
Messages
1
Reaction score
0
My Question is When Thread Culture is set to "ar-SA" and if i want to store date into DateTime variable as "en-US" then how do i achieve this.

I tried doing a simple experiment and code is as below.
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("1. Original String Date : {0}{1}", strDate, Environment.NewLine));

            DateTime d1 = Convert.ToDateTime(strDate);
            strList.Append(string.Format("2. 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("3. 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("4. 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("5. 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("6. 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;

            DateTime d5 = Convert.ToDateTime(strDate);
            strList.Append(string.Format("7. Thread Culture: {0} ->  DateTime d5 = Convert.ToDateTime(strDate);  returns : {1}{2}", Thread.CurrentThread.CurrentCulture.Name, d5.ToString(), Environment.NewLine));

            DateTime d6 = Convert.ToDateTime(strDate, CultureInfo.GetCultureInfo("en-US"));
            strList.Append(string.Format("8. Thread Culture: {0} ->         DateTime d6 = Convert.ToDateTime(strDate, CultureInfo.GetCultureInfo('en-US));  returns : {1}{2}", Thread.CurrentThread.CurrentCulture.Name, d6.ToString(), Environment.NewLine));



            tbResult.Text = strList.ToString();
        }

and above code throw below 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 d5 = Convert.ToDateTime(strDate); returns : 17/05/42 02:30:00 م
8. Thread Culture: ar-SA -> DateTime d6 = Convert.ToDateTime(strDate, CultureInfo.GetCultureInfo('en-US)); returns : 17/05/42 02:30:00 م



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.

and at point 7. I was expecting it would return "1/1/2021 2:30:00 PM" because of en-US culture, but it does not.

What is going wrong here? Has anyone faced similar issues? What is the solution ?

Regards
 
Last edited by a moderator:
Joined
Jul 3, 2021
Messages
37
Reaction score
2
That particular function call, is documented as not supported for conversion.
In fact, if you read the overloads, pretty much everything is marked as: "Calling this method always throws InvalidCastException."

If I were you, I'd cover those calls you're making under a try-catch exception. See if indeed InvalidCastException is thrown.
Then read the page and see if there is an edge case or an actual function which follows a certain format to actually use it.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,032
Latest member
OdellBerg4

Latest Threads

Top