【问题标题】:How can I get today's Jewish date in c#?如何在 c# 中获取今天的犹太日期?
【发布时间】:2011-09-08 21:18:12
【问题描述】:

我有这个代码:

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

我怎样才能得到今天的数字希伯来日期?

意思:

例如,我想知道某个特定的希伯来月份是否在这个月, 所以我必须将希伯来月份发送给函数 - 今天的月份和年份, 这样我就可以检查 dtAnyDateHebrew 是否等于 Today,大于。等等

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

有人可以帮我吗?

【问题讨论】:

  • 我希望你在这里得到一个很好的答案。如果你不这样做,你也可以试试新的Judaism StackExchange

标签: c# winforms date hebrew


【解决方案1】:

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

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..

就这么简单。

谢谢大家。

【讨论】:

  • 如果这解决了您的问题,您应该接受它作为答案。
【解决方案2】:

使用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);
}

【讨论】:

  • 太棒了! Shkoyach,西蒙!
【解决方案3】:

This blog entry 显示如何。

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 个字母)。然后我可以使用开关盒,但有两种:闰年和正常的。所以我问 - 有没有更简单的方法?
猜你喜欢
  • 2017-04-28
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
相关资源
最近更新 更多