0

OK I know the DateTime structure is very good at doing this.
But if you ever wanted to design your own calendar, or just wanted to know how things are possibly done, this could be a handy function. Enjoy.

public int dateDaycode(int year, int month, int day)
        {
            //Using Zeller's formula
            int d1, d2, d3, d4, code;
            if (month == 1)
            {
                month = 13; year--;
            }
            if (month == 2)
            {
                month = 14; year--;
            }
            d1 = year / 4;
            d2 = year / 100;
            d3 = year / 400;
            d4 = day + 2 * month + 3 * (month + 1) / 5 + year;
            code = (d4 + d1 - d2 + d3 + 1) % 7;
            return code;
            // code will be sun=0,mon=1,tue=2,wen=3,thu=4,fri=5,sat=6
        }
2
Contributors
1
Reply
39
Views
2 Years
Discussion Span
Last Post by xrj
1

Long time ago, by means of Bussiness Basic, I had this problem as "homework". It did was a headache and there was no Wikipedia nor Internet, just some colleagues comments that really did their best. Allow me to contribute with the following code:

        static void Main(string[] args)
        {
           System.Globalization.CultureInfo ci = 
               new System.Globalization.CultureInfo("en-US");
           System.Globalization.CultureInfo.DefaultThreadCurrentCulture = 
               ci;
           Int32 initYear = 1990;
           Int32 curYear = initYear;
           DateTime dt = new DateTime(curYear, 1, 1);
           DateTime dtStop = DateTime.Today;
           Int32 nErr1, nErr2, nDates;
            Int32 code0, code1, code2;
            do
            {
                Console.Write(dt.ToString("yyyy/MM/dd"));
                code0 = (Int32)dt.DayOfWeek;
                code1 = dateDaycode(dt.Year, dt.Month, dt.Day);
                if (code0 != code1)
                    nErr1++;
                code2 = dateDaycodeWiki(dt.Year, dt.Month, dt.Day);
                if (code0 != code2)
                    nErr2++;
                Console.WriteLine();
                dt = dt.AddDays(1);
                if (dt.Year != curYear)
                {
                    curYear++;
                    dt = new DateTime(curYear, 1, 1);
                }
                nDates++;
            } while (dt <= dtStop);
            if (nErr1 != 0)
                Console.WriteLine(nErr1.ToString() + " code 1 errors found ");
            if (nErr2 != 0)
                Console.WriteLine(nErr2.ToString() + " code 2 errors found ");
                   Console.WriteLine(nDates.ToString() + " total days between dates 01/01/" + 
                   initYear.ToString() + " and " + dtStop.ToShortDateString());
           Console.WriteLine("Press Enter to exit");
           Console.ReadLine();

        }
        public int dateDaycode(int year, int month, int day)
        {
            //Using Zeller's formula
            int d1, d2, d3, d4, code;
            if (month == 1)
            {
                month = 13; year--;
            }
            if (month == 2)
            {
                month = 14; year--;
            }
            d1 = year / 4;
            d2 = year / 100;
            d3 = year / 400;
            d4 = day + 2 * month + 3 * (month + 1) / 5 + year;
            code = (d4 + d1 - d2 + d3 + 1) % 7;
            return code;
            // code will be sun=0,mon=1,tue=2,wen=3,thu=4,fri=5,sat=6
        }
        public int dateDaycodeWiki(int year, int month, int day)
        {
        /* see http://es.wikipedia.org/wiki/Congruencia_de_Zeller
         or http://en.wikipedia.org/wiki/Zeller%27s_congruence */

            Single h;
            Int32 code;
            if (month == 1)
            {
                month = 13; year--;
            }
            if (month == 2)
            {
                month = 14; year--;
            }
            h = (day + (month + 1) * 13 / 5 + year + year / 4 + 6 * (year / 100) + year / 400) % 7;
            // h: 0=sat, 1=sun, 2=mon, ...., 6=friday
            code = (int) (Math.Floor(h) + 6) % 7;
            // code will be sun=0,mon=1,tue=2,wen=3,thu=4,fri=5,sat=6
        }
Votes + Comments
Allow me to upvote, albeit a bit late I guess. :)
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.