Get Next Hannukah Date In C #

I am trying to find out from today UTC the date of the next Gannuki. I already found that C # has a HebrewCalendar class, and I was able to get the current Jewish date with GetYear() , GetMonth() and GetDayOfMonth() . But I donโ€™t know how to work with this information to get a Jewish date that will be next after the current date.

Hannuk dates from the 25th anniversary of Kislev (3rd month on the Jewish calendar).

+5
source share
3 answers

@DmitryBychenko's answer is fine, although if you don't want to quote, you can also figure it out:

 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 HebrewCalendar , use this year, otherwise use the following

The result is also December 7, 2015 in the gregorian calendar

If (according to the comments) you do not want these pesky if conditions for some reason, you could do something like:

 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 it helps readability, but there you go

+3
source

As suggested on Twitter, here's the Noda Time solution :

 // 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); 

An 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 continue to request function 317 , it could be a lot easier. For instance:

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

Eh, just looping? Testing date one by one, starting with, say, DateTime.Now ?

  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; } } 

does it return result == 7 Dec 2015 ?

+1
source

All Articles