How to use DateTime.TryParse () for non-English languages ​​like Arabic?

I need to convert strings to DateTime objects that are in non-English. I saw many examples of converting DateTime to strings in other languages, but not vice versa.

This does not work:

CultureInfo provider = new CultureInfo("ar-AE"); // Arabic - United Arab Emirates string sample = "الاربعاء 16 مارس 2011"; // Arabic date in Gregorian calendar DateTime result; DateTime expected = new DateTime(2011, 3, 16); // the expected date bool b; b = DateTime.TryParse(sample, provider, DateTimeStyles.None, out result); Assert.IsTrue(b); Assert.AreEqual(expected, result); 

In addition, I need to handle strings that are in other calendars. This is what I tried and it does not work either.

 CultureInfo provider = new CultureInfo("ar-AE"); // Arabic - United Arab Emirates provider.DateTimeFormat.Calendar = new System.Globalization.HijriCalendar(); // Wednesday, March 16, 2011, 11 Rabi second in 1432 string sample = " ‏11 ربيع ثاني 1432 "; DateTime result; DateTime expected = new DateTime(2011, 3, 16); // ? bool b; b = DateTime.TryParse(sample, provider, DateTimeStyles.None, out result); Assert.IsTrue(b); Assert.AreEqual(expected, result); 

What am I missing?

+7
source share
3 answers

If you know the exact format, you can force it using TryParseExact :

 b = DateTime.TryParseExact(sample, "dddd d MMMM yyyy", provider, DateTimeStyles.None, out result); 

However, in your case this does not work. To find the problem, try making the return path:

 Console.WriteLine(expected.ToString("dddd d MMMM yyyy", provider)); 

And the result: "الأربعاء 16 مارس 2011", which (you can probably read which is better than me) differs from your input in one character: .NET uses (and expects) hamza, your input does not have it. If we modify the input in this way, everything will work:

 CultureInfo provider = new CultureInfo("ar-AE"); // Arabic - United Arab Emirates string sample = "الأربعاء 16 مارس 2011"; // Arabic date in Gregorian calendar DateTime result; DateTime expected = new DateTime(2011, 3, 16); // the expected date bool b; b = DateTime.TryParse(sample, provider, DateTimeStyles.None, out result); Assert.IsTrue(b); Assert.AreEqual(expected, result); 
+2
source
 DateTime result = DateTime.Parse("الاربعاء 16 مارس 2011", new CultureInfo("ar-JO")); 

But you can check the documentation: CultureInfo Class

+1
source

maybe something like this:

 int Year, DayOfMonth; string Month; string[] Months = new string[] {"ينایر","فبرایر","مارس","ابریل","مایو",...};//these texts are writen with persian keyboard,change the ی with ي ,its really hard with my keymap string[] Splits = Input.Split(" "); foreach(string Split in Splits) { if(Months.Contains(Split)) { Month = Months.IndexOf(Split); } else { int Number; if(int.TryParse(Split, out Number)) { if(Number<32) { DayOfMonth=Number; } else { Year=Number; } } } } 

if you are going to support multiple calendars:
you must add all months of calendars in order in this array.
after December there should be the following calendar months (rabi-ol-avval, rabi-ol-thani, ...)
then

int CalendarId = month / 12;
Month% = 12;

0
source

All Articles