简体   繁体   English

在C#中获得下一个Hannukah日期

[英]Get next Hannukah date in C#

I am trying to find out from today's UTC date the date of the next Hannukah. 我想从今天的UTC日期找出下一个Hannukah的日期。 I already found that C# has HebrewCalendar class and I was able to get the current Jewish date with GetYear() , GetMonth() and GetDayOfMonth() . 我已经发现C#有HebrewCalendar类,我能够通过GetYear()GetMonth()GetDayOfMonth()获得当前的犹太日期。 But don't really know how to work with this information to get the Jewish date that is gonna happen next for the current date. 但是,真的不知道如何使用这些信息来获得当前日期接下来会发生的犹太日期。

Hannukah is dated on 25th of Kislev (3rd month in Jewish calendar). Hannukah的日期是基斯莱夫25日(犹太历法的第3个月)。

@DmitryBychenko's answer is fine, although if you don't want to loop, you can also calculate it: @DmitryBychenko的答案很好,但如果你不想循环,你也可以计算它:

var calendar = new HebrewCalendar();
var result = DateTime.UtcNow;
if(
        calendar.GetMonth(result) < 3
    || (calendar.GetMonth(result)==3 && calendar.GetDayOfMonth(result)<25)
  )
   result = new DateTime(calendar.GetYear(result), 3, 25, calendar);
else
   result = new DateTime(calendar.GetYear(result)+1, 3, 25, calendar);

If you are under 25/3 on the HebrewCalendar , use this year, else use next 如果您在HebrewCalendar上HebrewCalendar ,请使用今年,否则请使用next

Result is also 7 Dec 2015 in the gregorian calendar 格里高利历中的结果也是2015年12月7日

If (as per the comments) you don't want those pesky if statements for some reason, you could do something like: 如果(根据评论)由于某种原因你不想要那些讨厌的 if语句,你可以这样做:

var calendar = new HebrewCalendar();
var result = DateTime.UtcNow;
var addYear = (calendar.GetMonth(result) < 3 || (calendar.GetMonth(result)==3 && calendar.GetDayOfMonth(result)<25)) ? 0 : 1;
result = new DateTime(calendar.GetYear(result) + addYear, 3, 25, calendar);

I don't think this helps readability but there you go 我不认为这有助于提高可读性,但你去了

As it was suggested on Twitter, here's a Noda Time solution: 正如在Twitter上建议的那样,这是一个Noda Time解决方案:

// As of 2.0, it will be CalendarSystem.HebrewCivil
var calendar = CalendarSystem.GetHebrewCalendar(HebrewMonthNumbering.Civil);
var today = SystemClock.Instance.InZone(DateTimeZone.Utc, calendar).Date;

var thisHannukah = new LocalDate(today.Year, 3, 25, calendar);
return thisHannukah >= today ? thisHannukah : thisHannukah.PlusYears(1);

Alternative for the last two statements: 替代最后两个陈述:

var year = today.Month < 3 || today.Month == 3 && today.Day <= 25
    ? today.Year : today.Year + 1;
return new LocalDate(year, 3, 25, calendar);

If we go ahead with feature request 317 , this could be much simpler. 如果我们继续使用功能请求317 ,这可能会更简单。 For example: 例如:

// Putative API only! Doesn't work yet!
MonthDay hannukah = new MonthDay(3, 25, calendar);
var nextHannukah = hannukah.NextOrSame(today);

Eh, just looping ? 呃,只是循环 Testing date one by one starting from, say, DateTime.Now ? DateTime.Now开始逐个测试date

  HebrewCalendar calendar = new HebrewCalendar();
  DateTime result = DateTime.Now;

  for (DateTime date = DateTime.Now.Date; ; date = date.AddDays(1)) {
    if (calendar.GetDayOfMonth(date) == 25 && calendar.GetMonth(date) == 3) {
      result = date;

      break;
    }
  }

it returns result == 7 Dec 2015 ? 它返回result == 7 Dec 2015

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM