显示本周周次:
// <summary>
/// 返回一个数组长度为3的字符串数组
/// </summary>
/// <returns>GetWeekDay[0]=周次;GetWeekDay[1]=该周第一天;GetWeekDay[2]=该周最后一天</returns>
public string[] GetWeekDay()
{
string[] inti = new string[3];
DateTime day = DateTime.Parse(DateTime.Today.Year + "-1-1");
System.DayOfWeek dateTime = day.DayOfWeek;
int DayCount = DateTime.Today.DayOfYear;
int i = (DayCount + aa(dateTime) - 2) / 7 + 1;
inti[0] = i.ToString();
inti[1] = day.AddDays(DayCount - 1).ToString("yyyy年MM月dd日");
inti[2] = day.AddDays(DayCount + 5).ToString("yyyy年MM月dd日");
Response.Write("第"+i+"周");
return inti;
}
private int aa(System.DayOfWeek weekday)
{
switch (weekday.ToString())
{
case "Sunday":
return 1;
case "Monday":
return 2;
case "Tuesday":
return 3;
case "Wednesday":
return 4;
case "Thursday":
return 5;
case "Friday":
return 6;
case "Saturday":
return 0;
default:
return 8;
}
}
//////////////////////////////////////////////////////////////////
C#显示本周日期收藏
/// <summary>
/// 计算本周起始日期(礼拜一的日期)
/// </summary>
/// <param name="someDate">该周中任意一天</param>
/// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns>
public static DateTime CalculateFirstDateOfWeek(DateTime someDate)
{
int i = someDate.DayOfWeek - DayOfWeek.Monday;
if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
return someDate.Subtract(ts);
}
/// <summary>
/// 计算本周结束日期(礼拜日的日期)
/// </summary>
/// <param name="someDate">该周中任意一天</param>
/// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns>
public static DateTime CalculateLastDateOfWeek(DateTime someDate)
{
int i = someDate.DayOfWeek - DayOfWeek.Sunday;
if(i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。
TimeSpan ts = new TimeSpan(i, 0, 0, 0);
return someDate.Add(ts);
}
/// <summary>
/// 判断选择的日期是否是本周(根据系统当前时间决定的‘本周’比较而言)
/// </summary>
/// <param name="someDate"></param>
/// <returns></returns>
public static bool IsThisWeek(DateTime someDate)
{
//得到someDate对应的周一
DateTime someMon = CalculateFirstDateOfWeek(someDate);
//得到本周一
DateTime nowMon = CalculateFirstDateOfWeek(DateTime.Now);
TimeSpan ts = someMon - nowMon;
if (ts.Days < 0)
ts = -ts;//取正
if (ts.Days >= 7)
{
return false;
}
else
{
return true;
}
}
/// <summary>
/// 本周起止时间
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
private string weekrange(System.DateTime dt)
{
int weeknow = Convert.ToInt32(dt.DayOfWeek);
int daydiff = (-1) * weeknow+1;
int dayadd = 7 - weeknow;
string datebegin = System.DateTime.Now.AddDays(daydiff).ToString("yyyy-MM-dd");
string dateend = System.DateTime.Now.AddDays(dayadd).ToString("yyyy-MM-dd");
return datebegin + " / " +dateend;
}
// 本月起止时间
private string months()
{
string moth = Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-")+"01";
string mothDay =Convert .ToDateTime (DateTime.Now).ToString("yyyy-MM-dd");
return moth + "/" + mothDay;
}
// 本年起止时间
private string years()
{
string year = Convert.ToDateTime(DateTime.Now).ToString("yyyy") + "-01-01";
string yearDay = Convert.ToDateTime(DateTime.Now).ToString("yyyy-MM-dd");
return year + "/" + yearDay;
}