[Mono-list] Console calendar

Paul Johnson paul at all-the-johnsons.co.uk
Tue Feb 28 22:36:07 UTC 2012


Hi,

I'm trying to piece together a simple calendar in C#. It is not a 
winform app, but a console application (so I can't just use the winform 
DatePicker).

I've come across a couple of C examples, with 
http://www.codingunit.com/how-to-make-a-calendar-in-c being the simplest 
to port over. Problem is that Feb started on a Wednesday and this comes 
up with a Monday for some odd reason.

Does anyone know a quick and simple way to do this in C#?

My code currently looks like this

     public class calendar
     {
         private List<int> daysInMonth;

         public calendar()
         {
             daysInMonth = new List<int>();
             daysInMonth.AddRange(new int[] { 0, 31, 28, 31, 30, 31, 30, 
31, 31, 30, 31, 30, 31 });
         }

         private int determineDayCode(int year)
         {
             Console.WriteLine("year = {0}", year);
             int d1 = (year - 1)/ 4;
             int d2 = (year - 1)/ 100;
             int d3 = (year - 1)/ 400;
             return Convert.ToInt32((year + d1 - d2 + d3) % 7);
         }

         private void checkForLeapYear(int year)
         {
             if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                 daysInMonth[2] = 29;
             else
                 daysInMonth[2] = 28;
         }

         private List<int> createCal(int ddc, int month, int year)
         {
             int day;
             List<int> cal = new List<int>();
             Console.WriteLine("1 + ddc * 5 = {0}", 1 + ddc * 5);
             for (day = 1; day <= 1 + ddc * 5; ++day)
                 cal.Add(0);
             for (day = 1; day <= daysInMonth[month]; ++day)
                 cal.Add(day);
             return cal;
         }

         public List<int> generateCalendar(int month, int year)
         {
             List<int> cal = new List<int>();
             int ddc = determineDayCode(year);
             checkForLeapYear(year);
             cal = createCal(ddc, month, year);
             return cal;
         }
     }

The calling methods passes in month and year and expects back a list. 
The idea is that it will put a 0 if the start of the week is not on that 
day (so for Feb, it should read 0,0,0,1 but it's coming out 0,1)

Thanks

Paul


More information about the Mono-list mailing list