c#根据年份和月份获得本月最后一天


获取一个月份中最早的一天和最后的一天。在C#的DateTime类中,已经提供了现成的函数,下面分别用两个简单的函数表示:

private DateTime GetFirstDayOfMonth(int Year,int Month)
        {
          //你见过不是从1号开始的月份么?没有
            //那么,直接返回给调用者吧!
            //良好的一个编程习惯就是你的代码让人家看了简单易懂
          
          return Convert.ToDateTime(Year.ToString() + "-" + Month.ToString() + "-1");
        }商账追收

private DateTime GetLastDayOfMonth(int Year, int Month)
        {
            //这里的关键就是 DateTime.DaysInMonth 获得一个月中的天数         
              int Days = DateTime.DaysInMonth(Year, Month);
            return Convert.ToDateTime(Year.ToString() + "-" + Month.ToString() + "-" + Days.ToString());
        }
原文地址:https://www.cnblogs.com/sky7034/p/2065449.html