Example #1
0
        private CalendarWeek CreateCalendarWeek(DateTime date)
        {
            CalendarWeek cw = new CalendarWeek()
            {
                Sunday    = date,
                Monday    = date.AddDays(1),
                Tuesday   = date.AddDays(2),
                Wednesday = date.AddDays(3),
                Thursday  = date.AddDays(4),
                Friday    = date.AddDays(5),
                Saturday  = date.AddDays(6)
            };

            return(cw);
        }
Example #2
0
        private IEnumerable <CalendarWeek> GenerateWeeks(DateTime date)
        {
            List <CalendarWeek> result = new List <CalendarWeek>();

            DateTime fom = GetFirstOfMonth(date);

            //get the most recent Sunday before fom
            DateTime sd = fom.AddDays(-(int)fom.DayOfWeek);

            //check if the first day of the month is a Sunday, if so add another row for the last week of the previous month
            //because we always want to see some days from the the previous month
            if (sd == fom)
            {
                sd = sd.AddDays(-7);
            }

            DateTime prevMonth = fom.AddMonths(-1);
            DateTime nextMonth = fom.AddMonths(1);

            while (sd < nextMonth)
            {
                CalendarWeek cw = CreateCalendarWeek(sd);
                result.Add(cw);
                sd = sd.AddDays(7);
            }

            //check if the last day of the month is a saturday, if so add another row for the 1st week of the next month
            //because we always want to see some days from the next month
            DateTime lom = nextMonth.AddDays(-1);

            if (lom.DayOfWeek == DayOfWeek.Saturday)
            {
                CalendarWeek cw = CreateCalendarWeek(nextMonth);
                result.Add(cw);
            }

            return(result);
        }