0

I have a specific problem with my calendar I'm making. Its difficult to explain but By determining the week#(of the month) and the dayofweek i make a grid. The dates are not lining up the right way. Example: October 1, 2011 should begin on a saturday, since the 1st is in week 1 but starts on a saturday, the calendar should look something like this: (assuming there's grid squares for each day) Iknow why this problem is occuring I just don't know how to fix it. The dates are not lining up where they should on the grid.

Calendar should look like this:

   Columns       October 2011    grid.column = 7 (0-6)  grid.row = 6 (0-5) (Sunday-Saturday)

     Sunday Monday Tuesday Wednesday Thursday Friday Saturday
row    0      1       2        3       4        5       6
0     ------------------------------------------------- 1st
1     2nd     3rd     4th     5th      6th-----------etc..--- 
2    --------------------------------------------etc...------
3    ------etc..---------------------------------------------
4    ---------------- 25th     26th    27th     28th    29th
5    30th     31st

But mine looks like this: (column 0-4 should be shifted down one row)

    Sunday Monday Tuesday Wednesday Thursday Friday Saturday
row    0      1       2        3       4        5       6
0     2nd     3rd     4th     5th      6th------------- 1st
1     9th     10th     11th   12th     13th-----------etc..--- 
2    --------------------------------------------etc...------
3    ------etc..------25th     26th    27th-----------------
4    30th     31st--- ---------------------     28th    29th
5    -------------

Code:

public SchedulePage(MainWindow parentForm)
{
    InitializeComponent();

    _parentForm = parentForm;

    // DateTime date = new DateTime(year, month, day);
    var t = new List<Schedule>();
    DateTime curr = DateTime.Now;
    DateTime newcurr = new DateTime(curr.Year, curr.Month, 1);
    var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
    var ms = cal.GetWeekOfYear(new DateTime(newcurr.Year, newcurr.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);

    for (var i = 1; newcurr.Month == curr.Month; newcurr = newcurr.AddDays(1))
    {
        var sched = new Schedule();
        var month_week = (newcurr.Day / 7) + 1;
        sched.MonthWeek = month_week.ToString();
        sched.Month = newcurr.Month.ToString();
        sched.Year = newcurr.Year.ToString();
        sched.day = newcurr.Day.ToString();
        sched.WeekOfYear = cal.GetWeekOfYear(newcurr, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
        sched.dayofweek = newcurr.DayOfWeek.ToString();
        t.Add(sched);

     /* if (newcurr.Day == 1 && (int)newcurr.DayOfWeek == 7)
        {
            int findspot = month_week - 1;

            //_parentForm.bindings.schedule.Add(new Schedule {
            //    WeekNo = month_week - findspot,
            //    WeekDay = (int)newcurr.DayOfWeek,
            //    day = newcurr.Day.ToString()
            //});
        } */

        _parentForm.bindings.schedule.Add(new Schedule {
            WeekNo = month_week - 1 ,
            WeekDay = (int)newcurr.DayOfWeek,
            day = newcurr.Day.ToString()
        });
    }
}
Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
TMan
  • 4,044
  • 18
  • 63
  • 117
  • Sorry my drawings didn't comeout the way I wanted it to but if you look at your calender for October 2011 you'll see there's 6 total rows, the first row (grid.row(0)) only contains saturday(oct.1st) but mine contains 2nd-6th and saturday the 1st. – TMan Oct 28 '11 at 00:51
  • You can use ImageShack to upload snapshots of what is happening and what you intend and use "Insert Image" option on stack overflow question editor by using the image shack picture link. – WPF-it Oct 28 '11 at 07:39

1 Answers1

3

You are using (day / 7) + 1 to get the WeekNo in that month (Row No). This will be incorrect unless the month begins on a Sunday.

Use the code found in this SO answer to get the correct week number in the month

It creates an ExtensionMethod for the DateTime class, which you can then use by calling newcurr.GetWeekOfMonth()

static class DateTimeExtensions {
    static GregorianCalendar _gc = new GregorianCalendar();
    public static int GetWeekOfMonth(this DateTime time) {
        DateTime first = new DateTime(time.Year, time.Month, 1);
        return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
    }

    static int GetWeekOfYear(this DateTime time) {
        return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
    }
}
Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490