#region 根据年月获取每周起止时间
public string RegexDay(string time)
{
Regex r = new Regex("-"); // Split on hyphens.
string[] s = r.Split(time);
return s[s.Length - 1].ToString();
}
public string GetWeekRange(System.DateTime dt)
{
int weeknow = Convert.ToInt32(dt.DayOfWeek);
int daydiff = (-1) * weeknow;
int dayadd = 6 - weeknow;
string dateBegin = dt.AddDays(daydiff).Date.ToString("yyyy-mm-dd");
string dateEnd = dt.AddDays(dayadd).Date.ToString("yyyy-mm-dd");
return dateBegin + " - " + dateEnd;
}
public void GetDay(int year, int month, ref string[] weekrangeStart, ref string[] weekrangeEnd)
{
//第一周
string firstdayofmonth = year + "-" + month + "-1";
weekrangeStart[0]=firstdayofmonth;
firstdayofmonth = GetWeekRange(Convert.ToDateTime(firstdayofmonth));
int firstweeklastday = Convert.ToInt32(RegexDay(firstdayofmonth));
weekrangeEnd[0] = year + "-" + month + "-" + firstweeklastday;
//第二周
weekrangeStart[1] = (Convert.ToDateTime(weekrangeEnd[0])).AddDays(1).Date.ToString("yyyy-MM-dd");
weekrangeEnd[1] = (Convert.ToDateTime(weekrangeStart[1])).AddDays(7 - 1).Date.ToString("yyyy-MM-dd");
//第三周
weekrangeStart[2] = (Convert.ToDateTime(weekrangeEnd[1])).AddDays(1).Date.ToString("yyyy-MM-dd");
weekrangeEnd[2] = (Convert.ToDateTime(weekrangeStart[2])).AddDays(7 - 1).Date.ToString("yyyy-MM-dd");
//第四周
weekrangeStart[3] = (Convert.ToDateTime(weekrangeEnd[2])).AddDays(1).Date.ToString("yyyy-MM-dd");
weekrangeEnd[3] = (Convert.ToDateTime(weekrangeStart[3])).AddDays(7 - 1).Date.ToString("yyyy-MM-dd");
//第五周
weekrangeStart[4] = (Convert.ToDateTime(weekrangeEnd[3])).AddDays(1).Date.ToString("yyyy-MM-dd");
weekrangeEnd[4] = (Convert.ToDateTime(weekrangeStart[4])).AddDays(7 - 1).Date.ToString("yyyy-MM-dd");
}
#endregion
public string RegexDay(string time)
{
Regex r = new Regex("-"); // Split on hyphens.
string[] s = r.Split(time);
return s[s.Length - 1].ToString();
}
public string GetWeekRange(System.DateTime dt)
{
int weeknow = Convert.ToInt32(dt.DayOfWeek);
int daydiff = (-1) * weeknow;
int dayadd = 6 - weeknow;
string dateBegin = dt.AddDays(daydiff).Date.ToString("yyyy-mm-dd");
string dateEnd = dt.AddDays(dayadd).Date.ToString("yyyy-mm-dd");
return dateBegin + " - " + dateEnd;
}
public void GetDay(int year, int month, ref string[] weekrangeStart, ref string[] weekrangeEnd)
{
//第一周
string firstdayofmonth = year + "-" + month + "-1";
weekrangeStart[0]=firstdayofmonth;
firstdayofmonth = GetWeekRange(Convert.ToDateTime(firstdayofmonth));
int firstweeklastday = Convert.ToInt32(RegexDay(firstdayofmonth));
weekrangeEnd[0] = year + "-" + month + "-" + firstweeklastday;
//第二周
weekrangeStart[1] = (Convert.ToDateTime(weekrangeEnd[0])).AddDays(1).Date.ToString("yyyy-MM-dd");
weekrangeEnd[1] = (Convert.ToDateTime(weekrangeStart[1])).AddDays(7 - 1).Date.ToString("yyyy-MM-dd");
//第三周
weekrangeStart[2] = (Convert.ToDateTime(weekrangeEnd[1])).AddDays(1).Date.ToString("yyyy-MM-dd");
weekrangeEnd[2] = (Convert.ToDateTime(weekrangeStart[2])).AddDays(7 - 1).Date.ToString("yyyy-MM-dd");
//第四周
weekrangeStart[3] = (Convert.ToDateTime(weekrangeEnd[2])).AddDays(1).Date.ToString("yyyy-MM-dd");
weekrangeEnd[3] = (Convert.ToDateTime(weekrangeStart[3])).AddDays(7 - 1).Date.ToString("yyyy-MM-dd");
//第五周
weekrangeStart[4] = (Convert.ToDateTime(weekrangeEnd[3])).AddDays(1).Date.ToString("yyyy-MM-dd");
weekrangeEnd[4] = (Convert.ToDateTime(weekrangeStart[4])).AddDays(7 - 1).Date.ToString("yyyy-MM-dd");
}
#endregion