开发者

How can I determine if a string is a Hebrew or Gregorian date?

开发者 https://www.devze.com 2022-12-29 12:42 出处:网络
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 obj

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消