Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get next Hannukah date in C#

I am trying to find out from today's UTC date the date of the next Hannukah. I already found that C# has HebrewCalendar class and I was able to get the current Jewish date with GetYear(), GetMonth()andGetDayOfMonth(). 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).

like image 449
Dracke Avatar asked Sep 10 '15 06:09

Dracke


1 Answers

@DmitryBychenko's answer is fine, although if you don't want to loop, you can also calculate it:

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

Result is also 7 Dec 2015 in the gregorian calendar

If (as per the comments) you don't want those pesky if statements 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 this helps readability but there you go

like image 144
Jcl Avatar answered Oct 12 '22 08:10

Jcl