asp.net 日期函数 某月的第一天和最后一天的日期
程序员文章站
2023-11-14 16:37:16
#region 得到一个月的第一天和最后一天的日期 /// /// 得到本月的第一天日期 /// ///...
#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
/// <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
推荐阅读