c#日期相关代码

//当年开始日期、结束日期
DateTime currentDate = DateTime.Now.Date;
DateTime yearStart = DateTime.Parse($"{currentDate.Year}-1-1");//开始日期

DateTime yearEnd = yearStart.AddYears(1).AddDays(-1).AddSeconds(-1);

int monthdays = System.Threading.Thread.CurrentThread.CurrentUICulture.Calendar.GetDaysInMonth(currentDate.Year, 12);//月天数


//当月开始日期、结束日期
DateTime monthStart = currentDate.AddDays(1 - currentDate.Day).Date;
DateTime monthEnd = currentDate.AddDays(1 - currentDate.Day).Date.AddMonths(1).AddSeconds(-1);
//上个月开始日期、结束日期

DateTime lastMonthStart = currentDate.AddMonths(-1).Date.AddDays(1 - currentDate.Day);
DateTime lastMonthEnd = DateTime.Parse(currentDate.AddDays(1 - currentDate.Day).ToShortDateString()).AddSeconds(-1);

posted @ 2021-05-24 17:51  苏苏苏en  阅读(175)  评论(0编辑  收藏  举报