问题描述
错误原因:从星期天至星期六算一周,二月份正确的是五周,但我显示出来是四周,求高手指点哪里写错了!!publicvoidbinds(){intyear,month;year=Convert.ToInt32(DropDownList1.SelectedValue);month=Convert.ToInt32(DropDownList2.SelectedValue);DateTimeweekstart=newDateTime(year,month,1);DateTimemonend=weekstart.AddMonths(1).AddDays(-1);inti=1;intdayofweek=Convert.ToInt32(weekstart.DayOfWeek.ToString("d"));DateTimeweekend=dayofweek==0?weekstart:weekstart.AddDays(7-dayofweek);stringstr1="第"+i+"个周";DropDownList3.Items.Add(newListItem(str1));while(weekend.AddDays(1)<monend){i++;weekstart=weekend.AddDays(1);weekend=weekend.AddDays(7)>monend?monend:weekend.AddDays(7);stringstr="第"+i+"个周";DropDownList3.Items.Add(newListItem(str));}}
解决方案
解决方案二:
//判断改天为此月的第几周weekstart=1(周1算第一天)weekstart=2(周日算第一天)protectedintWeekOfMonth(DateTimeday,intWeekStart){DateTimefirstofmonth;firstofmonth=Convert.ToDateTime(day.Date.Year+"-"+day.Date.Month+"-"+1);if(i==0){i=7;}if(WeekStart==1){return(day.Date.Day+i-2)/7+1;}if(WeekStart==2){return(day.Date.Day+i-2)/7;}return0;}