How can I determine if a string is a Hebrew or Gregorian date?
I have a string开发者_如何学Python value that contains or a Hebrew date, or a Gregorian date. How can I determine if it's Gregorian or Hebrew in C#?
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
}
精彩评论