如何在C#中获取今天的犹太日期?

15

我有这段代码:

DateTime dtAnyDateHebrew = new DateTime(5767, 1, 1, new System.Globalization.HebrewCalendar());

如何获取今天的希伯来数字日期?

意思是:

例如,我想知道某个特定的希伯来月份是否在本月中, 所以我需要将希伯来月份与今天的日期和年份一起发送到函数中, 这样我就能够检查dtAnyDateHebrew是否等于Today,是否大于等于Today等等。

最后我需要得到 - 今天的希伯来日期、今天的希伯来月份、今天的希伯来年份,作为整数(当然)。

有人能帮我吗?


我希望你在这里能得到一个好的答案。如果没有,你也可以尝试一下新的Judaism StackExchange - Isaac Moses
3个回答

12

好的,我找到了我需要的东西:

DateTime Today = DateTime.Today;

Calendar HebCal = new HebrewCalendar();
int curYear = HebCal.GetYear(Today);    //current numeric hebrew year
int curMonth = HebCal.GetMonth(Today);  //current numeric hebrew month

etc..

就是这么简单。

感谢你们所有人。


5
如果这解决了你的问题,你应该将其接受为答案。 - Dour High Arch

12

使用DateTime.Today并使用以下其中一种方法进行转换:

/// <summary>
/// Converts a gregorian date to its hebrew date string representation,
/// using custom DateTime format string.
/// </summary>
/// <param name="value">The <see cref="DateTime"/> value to convert.</param>
/// <param name="format">A standard or custom date-time format string.</param>
public static string ToJewishDateString(this DateTime value, string format)
{
  var ci = CultureInfo.CreateSpecificCulture("he-IL");
  ci.DateTimeFormat.Calendar = new HebrewCalendar();      
  return value.ToString(format, ci);
}

/// <summary>
/// Converts a gregorian date to its hebrew date string representation,
/// using DateTime format options.
/// </summary>
/// <param name="value">The <see cref="DateTime"/> value to convert.</param>
/// <param name="dayOfWeek">Specifies whether the return string should
/// include the day of week.</param>
public static string ToJewishDateString(this DateTime value, bool dayOfWeek)
{
  var format = dayOfWeek ? "D" : "d";
  return value.ToJewishDateString(format);
}

3
太棒了!谢谢,西蒙! - Veverke

3
这篇博客文章展示了如何实现希伯来日期的功能。
public static string GetHebrewJewishDateString(DateTime anyDate, bool addDayOfWeek)  { 
    System.Text.StringBuilder hebrewFormatedString = new System.Text.StringBuilder(); 

    // Create the hebrew culture to use hebrew (Jewish) calendar 
    CultureInfo jewishCulture = CultureInfo.CreateSpecificCulture("he-IL"); 
    jewishCulture.DateTimeFormat.Calendar = new HebrewCalendar(); 

    #region Format the date into a Jewish format 

   if (addDayOfWeek) 
   { 
      // Day of the week in the format " " 
      hebrewFormatedString.Append(anyDate.ToString("dddd", jewishCulture) + " "); 
   } 

   // Day of the month in the format "'" 
   hebrewFormatedString.Append(anyDate.ToString("dd", jewishCulture) + " "); 

   // Month and year in the format " " 
   hebrewFormatedString.Append("" + anyDate.ToString("y", jewishCulture)); 
   #endregion 

   return hebrewFormatedString.ToString(); 
}

好的,但您只得到了一个希伯来字符串,而函数需要int类型。另外,您会得到月份+年份作为一个字符串,而不是分开的。switch case语句有点复杂 - 我需要从任何字符中拆分出整个字符串,然后检查数组长度(有时希伯来月份名称有2个字母)。然后我可以使用switch case语句,但有两种类型:闰年和普通年。所以我想问 - 是否有更简单的方法? - Tal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接