Hi, I have a string value that contains or a Hebrew date, or a Gregorian date. How can I determine if it's Gregorian or Hebrew in C#?
+3
A:
You can use the TryParse
method of the DateTime
object - if it failed with the Hebrew culture you can then try with the Gregorian calendar:
DateTime myDate = DateTime.Now;
CultureInfo culture = CultureInfo.CreateSpecificCulture("he-IL");
culture.DateTimeFormat.Calendar = new HebrewCalendar(); // To be sure
DateTimeStyles styles = DateTimeStyles.None;
if (DateTime.TryParse("כ\"ה/אייר/תש\"ע", culture, styles, out myDate))
{
// Hebrew date
}
culture = CultureInfo.CreateSpecificCulture("en-US");
if (DateTime.TryParse("2/30/2010", culture, styles, out myDate))
{
// US date
}
Oded
2010-05-09 16:33:04
Have you actually tried this? DateTime.Now is not a method. Nor does the he-IL culture use the HebrewCalendar.
Hans Passant
2010-05-09 16:43:18
@Hans Passant - Thanks for pointing out my error with `DateTime.Now`. This has been corrected (and I added the `HebrewCalendar`). The code as is stands now works.
Oded
2010-05-09 17:05:12
It works perfectly, thank you!
TTT
2010-05-09 20:14:41
Any ideas why isn't this working on XP?
TTT
2010-05-13 19:16:39