DateTime beginDay = this.dateTimePicker1.Value;
DateTime endDay = this.dateTimePicker2.Value;
int n = 0;
for (DateTime dt = beginDay; dt <= endDay; dt = dt.AddDays(1))
{
if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
n++;
else
day++;
}
MessageBox.Show("当前有:" + n.ToString() + "个周未\r\n" + "合计有:" + day.ToString() + "平常日");
private void button1_Click(object sender, System.EventArgs e)
{
int day=0;
DateTime beginDay =DateTime.Parse(this.textBox1.Text);
DateTime endDay =DateTime.Parse(this.textBox2.Text);
System.TimeSpan Days;
Days=endDay-beginDay;
day=(int)Days.TotalDays;
int y=0; //从开始时间算,计算出第一个周六。
int x=0; //从开始时间算,计算出第一个周五。
switch(DateTime.Parse(this.textBox1.Text).DayOfWeek)
{
case DayOfWeek.Monday: //周1
y=-5;
x=-4;
break;
case DayOfWeek.Tuesday: //周2
y=-4;
x=-3;
break;
case DayOfWeek.Wednesday: //周3
y=-3;
x=-2;
break;
case DayOfWeek.Thursday: //周4
y=-2;
x=-1;
break;
case DayOfWeek.Friday: //周5
y=-1;
x=0;
break;
case DayOfWeek.Saturday: //周6
y=0;
x=-6;
break;
case DayOfWeek.Sunday: //周7
y=-6;
x=-5;
break;
}
int d= (day + y) / 7;
if((day + y) >=0)
{
d++;
}
int u= (day + x) / 7;
if((day + x) >=0)
{
u++;
}
MessageBox.Show("当前有:"+d.ToString() + "个周六\r\n" + "当前有:"+u.ToString() + "个周五\r\n" + "合计有:"+ (d + u) + "天");
}