Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a function which get hebrew numeric date and find the equal gregorian date, for example:
Hebrew: [3,1,5772] => 02.10.2011
I use:
DateTime DateInGreg = new DateTime(5772, 1, 3, new System.Globalization.HebrewCalendar());

My problem is when the hebrew year is a leap year, so that the month range goes between 1-13, but DateTime type is implemented to handle only monthes between 1-12 !
Try to switch today's hebrew date:
[It's a leap year..]
DateTime DateInGreg = new DateTime(5771, 13, 26, new System.Globalization.HebrewCalendar());
You'll get an ArgumentOutOfRangeException.

How can I solve this problem so that my function will be able to handle hebrew leap years also?
Thanks.
Posted
Updated 24-Sep-11 13:58pm
v2

This problem is similar to Ethiopian calender. In Ethiopian calender[^], a year contains 13 months, the first 12 months have 30 days,the last month has 5 or 6 days depends on the leap year. New year starts September 11 or 12 based on the leap year so and so forth. So the only solution to work with conversion of datetime from Gregorian to Ethiopian is to implement the System.Globalization.Calender abstract class. That is
C#
public sealed class EthiopianCalender : Calendar
    {
        public override DateTime AddMonths(DateTime time, int months)
        {
            throw new NotImplementedException();
        }

        public override int GetHour(DateTime time)
        {
            return base.GetHour(time);
        }

        public override int GetSecond(DateTime time)
        {
            return base.GetSecond(time);
        }

        public override int GetMinute(DateTime time)
        {
            return base.GetMinute(time);
        }

        public override int GetDaysInYear(int year)
        {
            return base.GetDaysInYear(year);
        }

        public override DateTime AddDays(DateTime time, int days)
        {
            return base.AddDays(time, days);
        }

        public override DateTime AddHours(DateTime time, int hours)
        {
            return base.AddHours(time, hours);
        }

        public override DateTime AddMilliseconds(DateTime time, double milliseconds)
        {
            return base.AddMilliseconds(time, milliseconds);
        }
        public override DateTime AddMinutes(DateTime time, int minutes)
        {
            return base.AddMinutes(time, minutes);
        }

        public override DateTime AddSeconds(DateTime time, int seconds)
        {
            return base.AddSeconds(time, seconds);
        }

        public override DateTime AddWeeks(DateTime time, int weeks)
        {
            return base.AddWeeks(time, weeks);
        }

        public override int GetLeapMonth(int year)
        {
            return base.GetLeapMonth(year);
        }

        public override bool IsLeapDay(int year, int month, int day)
        {
            return base.IsLeapDay(year, month, day);
        }

        public override int GetDaysInMonth(int year, int month)
        {
            return base.GetDaysInMonth(year, month);
        }
        public override bool IsLeapMonth(int year, int month)
        {
            return base.IsLeapMonth(year, month);
        }

        public override bool IsLeapYear(int year)
        {
            return base.IsLeapYear(year);
        }

        public override int GetLeapMonth(int year, int era)
        {
            return base.GetLeapMonth(year, era);
        }
        public override DateTime AddYears(DateTime time, int years)
        {
            throw new NotImplementedException();
        }
        public override double GetMilliseconds(DateTime time)
        {
            return base.GetMilliseconds(time);
        }

        public override int GetMonthsInYear(int year)
        {
            return base.GetMonthsInYear(year);
        }

        public override int GetWeekOfYear(DateTime time, CalendarWeekRule rule, DayOfWeek firstDayOfWeek)
        {
            return base.GetWeekOfYear(time, rule, firstDayOfWeek);
        }
        public override int[] Eras
        {
            get { throw new NotImplementedException(); }
        }

        public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
        {
            return base.ToDateTime(year, month, day, hour, minute, second, millisecond);
        }

        public override int ToFourDigitYear(int year)
        {
            return base.ToFourDigitYear(year);
        }
        public override int GetDayOfMonth(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override DayOfWeek GetDayOfWeek(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override int GetDayOfYear(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override int GetDaysInMonth(int year, int month, int era)
        {
            throw new NotImplementedException();
        }

        public override int GetDaysInYear(int year, int era)
        {
            throw new NotImplementedException();
        }

        public override int GetEra(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override int GetMonth(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override int GetMonthsInYear(int year, int era)
        {
            throw new NotImplementedException();
        }

        public override int GetYear(DateTime time)
        {
            throw new NotImplementedException();
        }

        public override bool IsLeapDay(int year, int month, int day, int era)
        {
            throw new NotImplementedException();
        }

        public override bool IsLeapMonth(int year, int month, int era)
        {
            throw new NotImplementedException();
        }

        public override bool IsLeapYear(int year, int era)
        {
            throw new NotImplementedException();
        }

        public override DateTime ToDateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int era)
        {
            throw new NotImplementedException();
        }
    }

For example a leap year calculation will be
C#
public override bool IsLeapYear(int year, int era)
   {
       try
       {
           if (year < 1)
           {
               throw new ArgumentOutOfRangeException("year", year, "Year is outside bounds");
           }
       }
       catch (Exception exception)
       {
           throw exception;        
       }
       return ((year % 4) == 3);
   }

Once you finished implement the calender based on Hebrew calender, you can use as the same way you use Gregorian calender. i.e

C#
EthiopianCalender calender = new EthiopianCalender();
DataTime dateTime = new DateTime(5771, 13, 26, calender.GetHour(Now), calender .GetMinute(Now), calender.GetSecond(Now), new EthiopianCalender())


I hope this will help you as a starting point.
 
Share this answer
 
v4
Comments
RaviRanjanKr 5-Nov-11 10:55am    
My 5+
Wonde Tadesse 5-Nov-11 17:58pm    
Thanks RaviRanjankr
Try using the calendar itself to do the conversion:
C#
System.Globalization.HebrewCalendar hc = new System.Globalization.HebrewCalendar();
DateTime gd = hc.ToDateTime(5771,13,26,0,0,0,0,System.Globalization.HebrewCalendar.CurrentEra);


C#
gd.ToString() == "9/25/2011 12:00:00 AM"
 
Share this answer
 
v2
Comments
RaviRanjanKr 5-Nov-11 10:55am    
My 5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900