c# 获取 本周、本月、本季度、本年 的开始时间或结束时间

时间:2020-12-06 14:41:19
        #region 获取 本周、本月、本季度、本年 的开始时间或结束时间
/// <summary>
/// 获取结束时间
/// </summary>
/// <param name="TimeType">Week、Month、Season、Year</param>
/// <param name="now"></param>
/// <returns></returns>
public static DateTime? GetTimeStartByType(string TimeType, DateTime now)
{
switch (TimeType)
{
case "Week":
return now.AddDays(-(int)now.DayOfWeek + );
case "Month":
return now.AddDays(-now.Day + );
case "Season":
var time = now.AddMonths( - ((now.Month - ) % ));
return time.AddDays(-time.Day + );
case "Year":
return now.AddDays(-now.DayOfYear + );
default:
return null;
}
} /// <summary>
/// 获取结束时间
/// </summary>
/// <param name="TimeType">Week、Month、Season、Year</param>
/// <param name="now"></param>
/// <returns></returns>
public static DateTime? GetTimeEndByType(string TimeType, DateTime now)
{
switch (TimeType)
{
case "Week":
return now.AddDays( - (int)now.DayOfWeek);
case "Month":
return now.AddMonths().AddDays(-now.AddMonths().Day + ).AddDays(-);
case "Season":
var time = now.AddMonths(( - ((now.Month - ) % ) - ));
return time.AddMonths().AddDays(-time.AddMonths().Day + ).AddDays(-);
case "Year":
var time2 = now.AddYears();
return time2.AddDays(-time2.DayOfYear);
default :
return null;
}
}
#endregion