DateHelper

public static class DateHelp
{
/// <summary>
/// 获取当前日期是该月的第几周
/// </summary>
/// <param name="dateNow"></param>
/// <returns>该日期所在周是第几周</returns>
public static int WeekOfMonth(DateTime dateNow)
{
DateTime StartDate = new DateTime(dateNow.Year, dateNow.Month, );
DateTime EndDate = StartDate.AddMonths().AddDays(-);
int WeekIndex = ;
TimeSpan ts = dateNow - StartDate;
DateTime TempDate = StartDate;
if (TempDate.DayOfWeek == System.DayOfWeek.Monday)
TempDate = TempDate.AddDays();
while (TempDate <= EndDate)
{
if (TempDate.DayOfWeek == System.DayOfWeek.Monday && dateNow.ToString("yyyy-MM-dd") != StartDate.ToString("yyyy-MM-dd"))
{
WeekIndex++;
}
if (TempDate.ToString("yyyy-MM-dd") == dateNow.ToString("yyyy-MM-dd"))
{
break;
}
TempDate = TempDate.AddDays();
} return WeekIndex; } /// <summary>
/// 获取当前日期所在周的周一日期
/// </summary>
/// <param name="dateNow"></param>
/// <returns></returns>
public static DateTime? GetStartWeekDate(DateTime dateNow)
{
for (int i = ; i < ; i++)
{
DateTime temp = dateNow.AddDays(i * (-));
if (temp.DayOfWeek == System.DayOfWeek.Monday)
return temp;
}
return null;
}
/// <summary>
/// 获取当前日期所在周的周日日期
/// </summary>
/// <param name="dateNow"></param>
/// <returns></returns>
public static DateTime? GetEndWeekDate(DateTime dateNow)
{
for (int i = ; i < ; i++)
{
DateTime temp = dateNow.AddDays(i);
if (temp.DayOfWeek == System.DayOfWeek.Sunday)
return temp;
}
return null;
}
}
上一篇:PHPCMS V9轻松完成WAP手机网站搭建全教程


下一篇:PostgreSQL 修改列报错:cannot be cast automatically to type integer