一个计算周次和本周时间范围的代码(c#)[转]
//当天与该周星期一相差的天数
private int getModdayNum(System.DayOfWeek dw)
{
int weeknow = Convert.ToInt32(dw);
int moddayNum = (-1) * weeknow+1;
return moddayNum;
}
//当天与该周星期日相差的天数
private int getSundayNum(System.DayOfWeek dw)
{
int weeknow = Convert.ToInt32(dw);
int sundayNum = 7-weeknow;
return sundayNum;
}
//该周星期一的日期
private string getWeekMonday(System.DayOfWeek dw,string timeFormateStr)
{
int weeknow = Convert.ToInt32(dw);
int moddayNum = (-1) * weeknow+1;
string weekMonday = System.DateTime.Now.AddDays(moddayNum).Date.ToString(timeFormateStr);
return weekMonday;
}
//该周星期日的日期
private string getWeekSunday(System.DayOfWeek dw,string timeFormateStr)
{
int weeknow = Convert.ToInt32(dw);
int sundayNum = 7-weeknow;
string weekSunday = System.DateTime.Now.AddDays(sundayNum).Date.ToString(timeFormateStr);
return weekSunday;
}
//本周是本年第几周
private int weekNum(System.DayOfWeek dw)
{
int weeknow = Convert.ToInt32(dw);//今天星期几
int daydiff = (-1) * (weeknow+1);//今日与上周末的天数差
int days = System.DateTime.Now.AddDays(daydiff).DayOfYear;//上周末是本年第几天
int weeks = days/7;
if(days%7 != 0)
{
weeks++;
}
return (weeks+2);
}
//调用方法
string datetime = getWeekMonday(System.DateTime.Now.DayOfWeek,"yyyy-MM-dd");
private int getModdayNum(System.DayOfWeek dw)
{
int weeknow = Convert.ToInt32(dw);
int moddayNum = (-1) * weeknow+1;
return moddayNum;
}
//当天与该周星期日相差的天数
private int getSundayNum(System.DayOfWeek dw)
{
int weeknow = Convert.ToInt32(dw);
int sundayNum = 7-weeknow;
return sundayNum;
}
//该周星期一的日期
private string getWeekMonday(System.DayOfWeek dw,string timeFormateStr)
{
int weeknow = Convert.ToInt32(dw);
int moddayNum = (-1) * weeknow+1;
string weekMonday = System.DateTime.Now.AddDays(moddayNum).Date.ToString(timeFormateStr);
return weekMonday;
}
//该周星期日的日期
private string getWeekSunday(System.DayOfWeek dw,string timeFormateStr)
{
int weeknow = Convert.ToInt32(dw);
int sundayNum = 7-weeknow;
string weekSunday = System.DateTime.Now.AddDays(sundayNum).Date.ToString(timeFormateStr);
return weekSunday;
}
//本周是本年第几周
private int weekNum(System.DayOfWeek dw)
{
int weeknow = Convert.ToInt32(dw);//今天星期几
int daydiff = (-1) * (weeknow+1);//今日与上周末的天数差
int days = System.DateTime.Now.AddDays(daydiff).DayOfYear;//上周末是本年第几天
int weeks = days/7;
if(days%7 != 0)
{
weeks++;
}
return (weeks+2);
}
//调用方法
string datetime = getWeekMonday(System.DateTime.Now.DayOfWeek,"yyyy-MM-dd");