System.Globalization.HebrewCalendar.GetDayDifference C# (CSharp) Method

GetDayDifference() private method

private GetDayDifference ( int lunarYearType, int month1, int day1, int month2, int day2 ) : int
lunarYearType int
month1 int
day1 int
month2 int
day2 int
return int
        int GetDayDifference(int lunarYearType, int month1, int day1, int month2, int day2) {
            if (month1 == month2) {
                return (day1 - day2);
            }

            // Make sure that (month1, day1) < (month2, day2)
            bool swap = (month1 > month2);
            if (swap) {
                // (month1, day1) < (month2, day2).  Swap the values.
                // The result will be a negative number.
                int tempMonth, tempDay;
                tempMonth = month1; tempDay = day1;
                month1 = month2; day1 = day2;
                month2 = tempMonth; day2 = tempDay;
            }

            // Get the number of days from (month1,day1) to (month1, end of month1)
            int days = m_lunarMonthLen[lunarYearType, month1] - day1;

            // Move to next month.
            month1++;

            // Add up the days.
            while (month1 < month2) {
                days += m_lunarMonthLen[lunarYearType, month1++];
            }
            days += day2;

            return (swap ? days : -days);
        }

Usage Example

Example #1
0
        private static DateTime HebrewToGregorian(int hebrewYear, int hebrewMonth, int hebrewDay, int hour, int minute, int second, int millisecond)
        {
            int num = hebrewYear - 3760;

            HebrewCalendar.__DateBuffer lunarDate = new HebrewCalendar.__DateBuffer();
            int lunarMonthDay = HebrewCalendar.GetLunarMonthDay(num, lunarDate);

            if (hebrewMonth == lunarDate.month && hebrewDay == lunarDate.day)
            {
                return(new DateTime(num, 1, 1, hour, minute, second, millisecond));
            }
            int dayDifference = HebrewCalendar.GetDayDifference(lunarMonthDay, hebrewMonth, hebrewDay, lunarDate.month, lunarDate.day);

            return(new DateTime(new DateTime(num, 1, 1).Ticks + (long)dayDifference * 864000000000L + Calendar.TimeToTicks(hour, minute, second, millisecond)));
        }