C# 获取本周最后一天(以星期天为最后一天)
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("本周最后一天:{0}", GetWeekLastDate(DateTime.Now));
Console.Read();
}
/// <summary>
/// 获取本周最后一天(以星期天为最后一天)
/// </summary>
/// <param name="dateTime">当前时间 DateTime.Now or DateTime.UtcNow</param>
/// <returns>后面的具体时、分、秒和传入值相等</returns>
public static DateTime GetWeekLastDate(DateTime dateTime)
{
int weeknow = Convert.ToInt32(dateTime.DayOfWeek);
weeknow = (weeknow == 0 ? 7 : weeknow);
int daydiff = (7 - weeknow);
// 本周最后一天
return Convert.ToDateTime(dateTime.AddDays(daydiff));
}
}
}