Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

c# - How do I use DateTime.TryParse() for non-English languages like Arabic?

I need to convert strings to DateTime objects that are in non-English languages. I've seen many examples of converting DateTime to strings in other languages, but not the other way around.

This doesn't seem to 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);

Additionally, I need to handle strings that are in other calendars. This is what I tried and it doesn't seem to 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
DateTime result = DateTime.Parse("???????? 16 ???? 2011", new CultureInfo("ar-JO"));

But you can check the documentation : CultureInfo Class


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...