C# 输入指定日期获取当前年的第一天 、当前年的最后天、某月的第一天 、某月的最后一天


方法

/// <summary>  
/// 取得当前年的第一天  
/// </summary>  
/// <param name="datetime">要取得月份第一天的时间</param>  
/// <returns></returns>  
private DateTime FirstDayOfYear(DateTime datetime)
{
    DateTime AssemblDate = Convert.ToDateTime(datetime.Year + "-" + "01" + "-" + "01");  // 组装当前指定月份
    return AssemblDate;  // 返回指定当前月份的第一天
}

/// <summary>  
/// 取得当前年的最后天  
/// </summary>  
/// <param name="datetime">要取得月份第一天的时间</param>  
/// <returns></returns>  
private DateTime LastDayOfYear(DateTime datetime)
{
    DateTime AssemblDate = Convert.ToDateTime(datetime.AddYears(1).Year + "-" + "01" + "-" + "01");  // 组装当前指定月份
    return AssemblDate.AddDays(-1);
}

/// <summary>  
/// 取得某月的第一天  
/// </summary>  
/// <param name="datetime">要取得月份第一天的时间</param>  
/// <returns></returns>  
private DateTime FirstDayOfMonth(DateTime datetime)
{
    return datetime.AddDays(1 - datetime.Day);
}

/// <summary>  
/// 取得某月的最后一天  
/// </summary>  
/// <param name="datetime">要取得月份最后一天的时间</param>  
/// <returns></returns>  
private DateTime LastDayOfMonth(DateTime datetime)
{
    return datetime.AddDays(1 - datetime.Day).AddMonths(1).AddDays(-1);

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM