How can I parse a Umm Al-Qura Arabic date string into a .NET DateTime object?

I have the following Arabic date in a Umm Al-Qura calendar that I want to parse in a .NET DateTime object:

الأربعاء, 17 ذو الحجة, 1436

This date is equivalent to September 30, 2015 in the Gregorian calendar.

I tried the following "standard" C # code to parse this date, but without success:

var cultureInfo = new CultureInfo("ar-SA"); cultureInfo.DateTimeFormat.Calendar = new UmAlQuraCalendar(); // the default one anyway var dateFormat = "dddd، dd MMMM، yyyy"; //note the ، instead of , var dateString = "‏الأربعاء‏، 17‏ ذو الحجة‏، 1436"; DateTime date; DateTime.TryParseExact(dateString, dateFormat, cultureInfo.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces, out date); 

No matter what I do, the result of TryParseExact always false . How to parse this string correctly in .NET?

By the way, if I start with a DateTime object, I can create the exact date string above using ToString() overload on DateTime without any problems. I just can't do it differently.

+6
source share
2 answers

Your length is 30 characters and contains four characters. UNICODE 8207 U+200F RIGHT TO LEFT MARK , but your date format is not.

 // This gives a string 26 characters long var str = new DateTime(2015,9,30).ToString(dateFormat, cultureInfo.DateTimeFormat) 

RIGHT TO LEFT MARK not a space.

If it contains only RLM / LRM / ALM, you probably should just delete them. Same thing with LRI / RLI / FSI and PDI isolate kits and LRE / RLE kits. You might not want to do this with LRO. LRO is often used with legacy data, where RTL characters are stored in the opposite order, that is, in order from left to right. In these cases, you can really change the characters.

Syncing dates from random places is a complex issue. You need a layered solution, try one method first, then another in order of priority until you succeed. However, there is no 100% solution, because people can print what they like.

See here for more information: http://www.unicode.org/reports/tr9/

+3
source

This is a culture from right to left, which means that the year will be shown first. For example, the following code:

 var cultureInfo = new CultureInfo("ar-SA"); cultureInfo.DateTimeFormat.Calendar = new UmAlQuraCalendar(); Console.WriteLine(String.Format(cultureInfo,"{0:dddd، dd MMMM، yyyy}",DateTime.Now)); 

manufactures الأربعاء، 17 ذو الحجة، 1436 . Parsing this line works without problems:

 var dateString="الأربعاء، 17 ذو الحجة، 1436"; var result=DateTime.TryParseExact(dateString, dateFormat, cultureInfo.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces,out date); Debug.Assert(result); 

PS: I don’t know how to write a format string for parsing the input, since changing the position of what looks like a comma for me changes the actual characters displayed in the string.

+2
source

All Articles