none
Hebrew date conversion in .net using HebrewCalendar() RRS feed

  • Question

  • I am trying to get Hebrew anniversary date for any specific date using  HebrewCalendar() library. It is giving incorrect anniversary date for some dates. Example: For the date: 2018-03-22 anniversary date: April 11, 2019. But below program gives incorrect date: March 13, 2019

    HebrewCalendar HebCal = new HebrewCalendar();

    DateTime dtAnyDateEnglish;

    dtAnyDateEnglish = DateTime.Parse("2018-03-22");

    int hebYear = HebCal.GetYear(dtAnyDateEnglish);

    int hebMonth = HebCal.GetMonth(dtAnyDateEnglish);

    int hebDay = HebCal.GetDayOfMonth(dtAnyDateEnglish);

    DateTime DT = new HebrewCalendar().ToDateTime(HebCal.GetYear(DateTime.Today), hebMonth, hebDay, 0, 0, 0, 0, System.Globalization.HebrewCalendar.CurrentEra);

    Console.WriteLine(DT.ToString());

    Please help to find correct anniversary date using .net library.

    Thursday, March 7, 2019 9:53 PM

All replies

  • For those of us who do not understand what a Hebrew anniversary date is can you define the rules that says given date X the anniversary date is Y? 


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 8, 2019 4:07 PM
    Moderator
  • I dont know the rule. I thought using dot net library HebrewCalendar(), I will can get hebrew anniversary date of any English date. But for some reason it does not give correct date for some specific dates. For example for the date "2018-03-22" anniversary date should be April 11, 2019. But HebrewCalendar() gives incorrect date: March 13, 2019

    Here is url to get anniversary date for any english date: http://international.aish.com/kaddish/yahrzeit_calendar.php

    I like to get same anniversary date using .net library. Any help is appreciated.

    Friday, March 8, 2019 6:22 PM
  • The link you gave was talking about Yahrzeit dates.  I went to this site (https://www.hebcal.com/yahrzeit/) and it does mention an anniversary date. When I put in the date you gave it prints the same value for both anniversary and Yahrzeit. I don't know the difference.

    I looked around and I cannot find any place where an actual algorithm is provided. Every reference is to an online API that does the calculation for you. Given this I wonder if it requires looking up data in a database or something such that simply using the calendar isn't sufficient.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, March 8, 2019 7:12 PM
    Moderator
  • Thank for your reply.

    https://www.hebcal.com/yahrzeit/

    This site has some resource for developer to get Yahrzeit anniversaries date but not any algorithm. 

    Saturday, March 9, 2019 3:07 PM
  • Yes I went to that site unfortunately it uses an API so I have no way of determining how the algorithm works. You mentioned your code isn't coming up with the correct value, can you post it?

    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, March 9, 2019 6:49 PM
    Moderator
  • Here is c# code. Most of the time this code works to get anniversary date in current year. But does not work for specific dates like "2018-03-22"

    HebrewCalendar HebCal = new HebrewCalendar();

    DateTime dtAnyDateEnglish;

    dtAnyDateEnglish = DateTime.Parse("2018-03-22");

    int hebYear = HebCal.GetYear(dtAnyDateEnglish);

    int hebMonth = HebCal.GetMonth(dtAnyDateEnglish);

    int hebDay = HebCal.GetDayOfMonth(dtAnyDateEnglish);

    DateTime DT = new HebrewCalendar().ToDateTime(HebCal.GetYear(DateTime.Today), hebMonth, hebDay, 0, 0, 0, 0, System.Globalization.HebrewCalendar.CurrentEra);

    Console.WriteLine(DT.ToString());

    Saturday, March 9, 2019 9:32 PM
  • I modified your code to allow for testing more values. I then compared them to what hebcal was returning and most of the values are different. The February and April days line up but others don't. 

    static void Main ( string[] args )
        {
            var hebrewCalendar = new HebrewCalendar();
    
            var values = new []
            {
                "3/4/2018",
                "3/22/2017",
                "3/22/2018",
                "2/1/2018",
                "4/10/2016",
                "12/25/2016"
            };
    
            Console.WriteLine("Gregorian\tAnniversary");
            Console.WriteLine();
    
            var anniversaryYear = hebrewCalendar.GetYear(DateTime.Today);
    
            foreach (var value in values)
            {
                var date = DateTime.Parse(value);
    
                var hebYear = hebrewCalendar.GetYear(date);
                var hebMonth = hebrewCalendar.GetMonth(date);
                var hebDay = hebrewCalendar.GetDayOfMonth(date);
                    
                var anniversaryDate = hebrewCalendar.ToDateTime(anniversaryYear, hebMonth, hebDay, 0, 0, 0, 0, System.Globalization.HebrewCalendar.CurrentEra);
    
                Console.WriteLine($"{date.ToShortDateString()}\t{anniversaryDate.ToShortDateString()}");
            };
    
            Console.ReadLine();
        }
    Gregorian       Anniversary
    
    3/4/2018        2/22/2019
    3/22/2017       3/1/2019
    3/22/2018       3/13/2019
    2/1/2018        1/22/2019
    4/10/2016       4/7/2019
    12/25/2016      12/3/2018

    Values from Hebcal.

    3/24/2019
    3/31/2019
    4/11/2019
    1/22/2019
    4/7/2019
    12/23/2019
    I looked at a couple of variants like using day of year to calculate the correct value and converting the anniversary date back to Gregorian but nothing returned the results that the calculator did. Finding the algorithm would really help here because I suspect it isn't a simple calculation but rather takes other things into account such as the date relative to beginning/end of calendar year or possibly relative to today.


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, March 9, 2019 10:31 PM
    Moderator