繁体   English   中英

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

[英]C#.Net Getting Last day of the Previous Month from current date

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

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

var lastDayLastMonth = firstDayCurrentMonth.AddDays(-1);
DateTime now = DateTime.Now;
DateTime lastDayOfLastMonth = now.Date.AddDays(-now.Day);

尝试DateTime.DaysInMonth(int year,int month)方法

这是一个例子:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM