C#.Net从当前日期开始上个月的最后一天

时间:2009-07-16 14:49:19

标签: c# .net

快速表达任何人都可以吗?

3 个答案:

答案 0 :(得分:55)

var now = DateTime.Now;    
var firstDayCurrentMonth = new DateTime(now.Year, now.Month, 1);

var lastDayLastMonth = firstDayCurrentMonth.AddDays(-1);

答案 1 :(得分:15)

DateTime now = DateTime.Now;
DateTime lastDayOfLastMonth = now.Date.AddDays(-now.Day);

答案 2 :(得分:3)

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

以下是一个例子:

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