我如何确定字符串是希伯来语还是格里高利日期?
-
04-10-2019 - |
题
我有一个包含或希伯来日期或格里高利日期的字符串值。我如何确定C#中的Gregorian还是Hebrew?
解决方案
您可以使用 TryParse
方法的方法 DateTime
对象 - 如果它因希伯来文化而失败,您可以尝试使用Gregorian日历:
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
}
不隶属于 StackOverflow