我试图在C#中使用DateTime函数来计算下个月的最后一天.
例如,今天是2015年12月17日.我希望DateTime函数返回2016年1月31日(下个月的最后一天).
我使用以下来计算下个月的第一天(这是有效的):
DateTime firstDayNextMonth = DateTime.Today.AddDays(-DateTime.Now.Day+1).AddMonths(1);
Ben Voigt.. 11
DateTime reference = DateTime.Now; DateTime firstDayThisMonth = new DateTime(reference.Year, reference.Month, 1); DateTime firstDayPlusTwoMonths = firstDayThisMonth.AddMonths(2); DateTime lastDayNextMonth = firstDayPlusTwoMonths.AddDays(-1); DateTime endOfLastDayNextMonth = firstDayPlusTwoMonths.AddTicks(-1);
演示:http://rextester.com/AKDI52378
DateTime reference = DateTime.Now; DateTime firstDayThisMonth = new DateTime(reference.Year, reference.Month, 1); DateTime firstDayPlusTwoMonths = firstDayThisMonth.AddMonths(2); DateTime lastDayNextMonth = firstDayPlusTwoMonths.AddDays(-1); DateTime endOfLastDayNextMonth = firstDayPlusTwoMonths.AddTicks(-1);
演示:http://rextester.com/AKDI52378