asp.net 日期函數 某月的第一天和最後一天的日期
來源:互聯網
上載者:User
#region 得到一個月的第一天和最後一天的日期
/// <summary>
/// 得到本月的第一天日期
/// </summary>
/// <returns>DateTime</returns>
public static DateTime GetFirstDayOfMonth()
{
return GetFirstDayOfMonth(DateTime.Now);
}
/// <summary>
/// 得到本月的最有一天的日期
/// </summary>
/// <returns>DateTime</returns>
public static DateTime GetLastDayOfMonth()
{
return GetLastDayOfMonth(DateTime.Now);
}
/// <summary>
/// 得到一個月的第一天
/// </summary>
/// <param name="someday">這個月的隨便一天</param>
/// <returns>DateTime</returns>
public static DateTime GetFirstDayOfMonth(DateTime someday)
{
int totalDays = DateTime.DaysInMonth(someday.Year, someday.Month);
DateTime result;
int ts = 1 - someday.Day;
result = someday.AddDays(ts);
return result;
}
/// <summary>
/// 得到一個月的最後一天
/// </summary>
/// <param name="someday">這個月的隨便一天</param>
/// <returns>DateTime</returns>
public static DateTime GetLastDayOfMonth(DateTime someday)
{
int totalDays = DateTime.DaysInMonth(someday.Year, someday.Month);
DateTime result;
int ts = totalDays - someday.Day;
result = someday.AddDays(ts);
return result;
}
#endregion