views:

152

answers:

1

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
Have you actually tried this? DateTime.Now is not a method. Nor does the he-IL culture use the HebrewCalendar.
Hans Passant
@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
It works perfectly, thank you!
TTT
Any ideas why isn't this working on XP?
TTT