代码之家  ›  专栏  ›  技术社区  ›  Julius A

C.NET从当前日期开始获取上个月的最后一天

  •  13
  • Julius A  · 技术社区  · 16 年前

    有人请快速表达一下吗?

    3 回复  |  直到 8 年前
        1
  •  52
  •   Seb Nilsson    8 年前
    var now = DateTime.Now;    
    var firstDayCurrentMonth = new DateTime(now.Year, now.Month, 1);
    
    var lastDayLastMonth = firstDayCurrentMonth.AddDays(-1);
    
        2
  •  14
  •   LukeH    16 年前
    DateTime now = DateTime.Now;
    DateTime lastDayOfLastMonth = now.Date.AddDays(-now.Day);
    
        3
  •  2
  •   Richard McGuire    16 年前

    试试 DateTime.DaysInMonth(int year, int month) method

    下面是一个例子:

    DateTime oneMonthAgo = DateTime.Now.AddMonths(-1);
    int days = DateTime.DaysInMonth(oneMonthAgo.Year, oneMonthAgo.Month);