I'm using below code for convert string to datetime(with slected language). It works well for all other language except Arabic. When passing ar as CultureInfo it throws an error "String was not recognized as a valid DateTime". What is the reason of it?
CultureInfo ci = new CultureInfo("ar"); //works well for en,de,fr,hi,ru string mDate = "2018-07-15 17:00"; DateTime userTime = Convert.ToDateTime(mDate,ci);
The main problem is that the "ar" culture has a different default calendar system - it's trying to parse that as a date in the Um Al Qura calendar. If you print
ci.DateTimeFormat.Calendar it will show
System.Globalization.UmAlQuraCalendar, whereas the value you've shown is presumably in the Gregorian calendar system. So even if the value could be parsed, it wouldn't represent the value you'd expect it to.
There's a secondary problem that you're using the default formats supported by the "ar" culture - that's not a good idea unless you know that the value was definitely provided as a human readable value for that specific culture.
Given that the value is in ISO-8601 format, it looks like it's actually intended to be machine readable rather than human readable - so I'd suggest parsing using
CultureInfo.InvariantCulture (and ideally using
DateTime.ParseExact rather than
Convert.ToDateTime). You can then decide whether you want to display the value to the user in their calendar system or not.
See more on this question at Stackoverflow