显示日期是当前区间第几周?
代码
/// <summary>
/// 显示日期是当前区间第几周?
/// </summary>
/// <param name="day">当前日期</param>
/// <param name="datespanGuid"></param>
/// <returns></returns>
public int GetCurrentWeek(DateTime day, Guid datespanGuid)
{
CDateSpan span = db.CDateSpan.SingleOrDefault(u => u.DateSpanGuid == datespanGuid);
if (span != null && span.EndTime.Subtract(day).Days >= 0 && day.Subtract(span.BeginTime).Days >= 0)
{
int currentWeek = 1;
//计算出第一周周末(星期六)的日期
DateTime beginSaturday = span.BeginTime.Add(new TimeSpan(span.Offset, 0, 0, 0)); //span.BeginTime-span.Offset:起始周开始计算的时间
int saturdaySpan = 6 - (int)(beginSaturday.DayOfWeek); //这天离周末还有几天
beginSaturday = beginSaturday.Add(new TimeSpan(saturdaySpan, 0, 0, 0)); // 周末的日期
//
int days = day.Subtract(beginSaturday).Days;
if (days > 0)
{
currentWeek += days / 7 + 1;
}
return currentWeek;
}
else
{
return -1;//区间不存在或者日期不在区间中
}
}
/// 显示日期是当前区间第几周?
/// </summary>
/// <param name="day">当前日期</param>
/// <param name="datespanGuid"></param>
/// <returns></returns>
public int GetCurrentWeek(DateTime day, Guid datespanGuid)
{
CDateSpan span = db.CDateSpan.SingleOrDefault(u => u.DateSpanGuid == datespanGuid);
if (span != null && span.EndTime.Subtract(day).Days >= 0 && day.Subtract(span.BeginTime).Days >= 0)
{
int currentWeek = 1;
//计算出第一周周末(星期六)的日期
DateTime beginSaturday = span.BeginTime.Add(new TimeSpan(span.Offset, 0, 0, 0)); //span.BeginTime-span.Offset:起始周开始计算的时间
int saturdaySpan = 6 - (int)(beginSaturday.DayOfWeek); //这天离周末还有几天
beginSaturday = beginSaturday.Add(new TimeSpan(saturdaySpan, 0, 0, 0)); // 周末的日期
//
int days = day.Subtract(beginSaturday).Days;
if (days > 0)
{
currentWeek += days / 7 + 1;
}
return currentWeek;
}
else
{
return -1;//区间不存在或者日期不在区间中
}
}