【问题标题】:C# convert Hebrew calendar date to Gregorian calendar dateC# 将希伯来日历日期转换为公历日期
【发布时间】:2020-02-01 22:11:59
【问题描述】:

我想将希伯来日期(犹太)转换为公历; 我该怎么做? 我试过了,但它不起作用:

string birth = txtBearthDay.Text+" "+ txtBearthMonth.Text+" " + txtBearthYear.Text;
HebrewCalendar hc = new HebrewCalendar();
CultureInfo jewishCulture = CultureInfo.CreateSpecificCulture("he-IL");

jewishCulture.DateTimeFormat.Calendar = hc;
dt = DateTime.Parse(birth,jewishCulture.DateTimeFormat);

【问题讨论】:

  • 使用日期是一种痛苦。能把人逼疯的那种:youtube.com/watch?v=-5wpm-gesOY |话虽如此,对于 Unix 纪元 (1.1.1970) 开始之后的日期,.NET 具有 exceptional 支持。通常,诸如 Callendar、Culture 和 Formats 之类的东西都是从 Windows 中提取的。但当然你可以覆盖它。我实际上找到了这个小例子:stackoverflow.com/a/19075881/3346583
  • 限制的原因是,内部所有 taht DateTime 保存的是自 Unix 纪元开始以来的 Ticks 数。所有其他属性——包括字符串表示——只是对该值的解释。对于 Unix 纪元之前的日期,它变为 Historian Hard。
  • 您可以在代码中将Bearth 的拼写更正为Birth,这样不会让人分心吗?
  • 显然人们讨厌我给你相关的背景信息,所以我不得不删除我的答案。

标签: c# .net


【解决方案1】:

试试string date_in_oth_culture = dt.ToString(new CultureInfo("he-IL");。 我希望它有所帮助。


编辑:抱歉,我误解了你的意思。你可以找到更多关于如何使用HebrewCalendarhere的信息。
编辑 2: 不幸的是,文档上的代码似乎都不起作用,在互联网上我找不到任何东西。

一种解决方案可能是使用 HebrewCalendar 计算新日期,然后使用 switch 语句自行翻译。

HebrewCalendar hc = new HebrewCalendar();
CultureInfo culture = CultureInfo.CreateSpecificCulture("he-IL");
culture.DateTimeFormat.Calendar = hc;

DateTime hebrDate = hc.ToDateTime(year, month, day, hour, minute, second, millisecond, era); // Converts Hebrew date into Gregorian date


DateTime gregDate = new DateTime(2020, 2, 1); // Gregorian date

string hebrMonth;
switch(hc.GetMonth(gregDate))
{
  case 1:
    hebrMonth = "___";
    break;
  case 2:
    hebrMonth = "___";
    break;
  // And so on
}

string finalDate = hc.getDayOfMonth(gregDate) + " " + hebrMonth + " " + hc.getYear(gregDate) + " " + hc.getEra(gregDate);

希望对你有帮助。

【讨论】:

  • 尝试类似:var dob = DateTime.Parse("[Gregorian DateTime]", CultureInfo.InvariantCulture); var hc = new HebrewCalendar(); var hebrewDate = new DateTime(hc.GetYear(dob), hc.GetMonth(dob), hc.GetDayOfMonth(dob));
  • @Jimi 我试过你的代码,但它没有回答最初的问题(希伯来语 -> gregorian),也没有解决我的答案中的问题(据我所知)。如果我错了,请告诉我(解释我错了什么或错在哪里,并可能提出解决建议)。
  • 那是希伯来日期时间,现在你只需从它创建一个新的日期时间~以同样的方式:var gregorianDate = new DateTime(hebrewDate.Year, hebrewDate.Month, hebrewDate.Day, hc);(hc 是在上一条评论中创建的希伯来日历:)。所以你有两种方式。
【解决方案2】:

实际上非常简单:

new DateTime(5780, 5, 7, new HebrewCalendar())

这将返回具有公历属性值 (2020-02-02) 的 DateTime 对象,因为 DateTime 的属性是使用公历呈现的。如果要从 DateTime 中取回希伯来日历值,则必须显式查询 HebrewCalendar 类。

【讨论】:

    猜你喜欢
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多