C#获取周一、周日的日期
DateTime dt = DateTime.Now; //当前时间
DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一
DateTime endWeek = startWeek.AddDays(6); //本周周日
DateTime startMonth = dt.AddDays(1 - dt.Day); //本月月初
DateTime endMonth = startMonth.AddMonths(1).AddDays(-1); //本月月末
//DateTime endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1); //本月月末
DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day); //本季度初
DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1); //本季度末
DateTime startYear = new DateTime(dt.Year, 1, 1); //本年年初
DateTime endYear = new DateTime(dt.Year, 12, 31); //本年年末
第几周:
public int GetWeekOfCurrDate(DateTime dt)
{
int Week = 1;
int nYear = dt.Year;
System.DateTime FirstDayInYear = new DateTime(nYear, 1, 1);
System.DateTime LastDayInYear = new DateTime(nYear, 12, 31);
int DaysOfYear = Convert.ToInt32(LastDayInYear.DayOfYear);
int WeekNow = Convert.ToInt32(FirstDayInYear.DayOfWeek) - 1;
if (WeekNow < 0) WeekNow = 6;
int DayAdd = 6 - WeekNow;
System.DateTime BeginDayOfWeek = new DateTime(nYear, 1, 1);
System.DateTime EndDayOfWeek = BeginDayOfWeek.AddDays(DayAdd);
Week = 2;
for (int i = DayAdd + 1; i <= DaysOfYear; i++)
{
BeginDayOfWeek = FirstDayInYear.AddDays(i);
if (i + 6 > DaysOfYear)
{
EndDayOfWeek = BeginDayOfWeek.AddDays(DaysOfYear - i - 1);
}
else
{
EndDayOfWeek = BeginDayOfWeek.AddDays(6);
}
if (dt.Month == EndDayOfWeek.Month && dt.Day <= EndDayOfWeek.Day)
{
break;
}
Week++;
i = i + 6;
}
return Week;
}
//
//构造当前月的1号
DateTime dd = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
//获得当前月1号是一个星期的第几天
int week = (int)dd.DayOfWeek;
//用来存储,当第一天不是星期天时,这个星期还有多少天
int t1 = 0;
if (week > 0)
{
//如果不是一个星期日
currentWeek++;
t1 = 7 - week;
}
int t2 = (DateTime.Now.Day - t1) % 7;
int t3 = 0;
if (t2 != 0)
{
t3 = (DateTime.Now.Day - t1) / 7 + 1;
}
else
{
t3 = (DateTime.Now.Day - t1) / 7;
}
currentWeek += t3;