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.
- Original String Date : 2021-01-01T14:30:00+05:30
- Thread Culture: en-US -> DateTime d1 = Convert.ToDateTime(strDate); returns : 1/1/2021 2:30:00 PM
- Thread Culture: en-US -> DateTime d2 = Convert.ToDateTime(strDate, CultureInfo.GetCultureInfo('ar-SA')); returns : 1/1/2021 2:30:00 PM
- Thread Culture: en-US -> DateTime d3 = Convert.ToDateTime(strDate, CultureInfo.GetCultureInfo('ja-JP')); returns : 1/1/2021 2:30:00 PM
- Thread Culture: en-US -> var ss = d1.ToString(CultureInfo.GetCultureInfo('ar-SA')); returns : 17/05/42 02:30:00 م
- Thread Culture: en-US -> DateTime d4 = Convert.ToDateTime(d1, CultureInfo.GetCultureInfo('ar-SA')); returns : 1/1/2021 2:30:00 PM
- 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