你的位置:首页 > ASP.net教程

[ASP.net教程](转)C#根据当前时间获取周,月,季度,年度等时间段的起止时间


DateTime dt = DateTime.Now; //当前时间DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一DateTime endWeek = startWeek.AddDays(6); //本周周日DateTime startMonth = dt.AddDays(1 - dt.Day); //本月月初DateTime endMonth = startMonth.AddMonths(1).AddDays(-1); //本月月末//endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1); //本月月末DateTime startQuarter = dt.AddMonths(0 - (dt.Month - 1) % 3).AddDays(1 - dt.Day); //本季度初DateTime endQuarter = startQuarter.AddMonths(3).AddDays(-1); //本季度末DateTime startYear = new DateTime(dt.Year, 1, 1); //本年年初DateTime endYear = new DateTime(dt.Year, 12, 31); //本年年末至于昨天、明天、上周、上月、上季度、上年度等等,var 上周一 = DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) - 7);    //上周一var 上周末 = DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) - 7).AddDays(6);   //上周末(星期日)//下周var 下周一 = DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) + 7);    //下周一var 下周末 = DateTime.Now.AddDays(Convert.ToInt32(1 - Convert.ToInt32(DateTime.Now.DayOfWeek)) + 7).AddDays(6); //下周末 DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天//巧用C#里ToString的字符格式化更简便DateTime.Now.ToString("yyyy-MM-01");//本月初DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1).ToShortDateString();//本月最后一天DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();//上个月1号DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();//上个月最后一天DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString();// 下个月1号DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortDateString();//下下月最后一天DateTime.Now.AddDays(7).ToShortDateString();//7天后DateTime.Now.AddDays(-7).ToShortDateString();//7天前DateTime.Now.Date.ToShortDateString();//本年度,用ToString的字符格式化我们也很容易地算出本年度的第一天和最后一天DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();//本年度第一天DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortDateString();//本年度最后一天DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString(); //上年度第一天, DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();//上年度第最后一天, DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString(); //下年度第一天DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();//下年度最后一天//本季度,DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day);//本季度第一天; DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();//本季度的最后一天DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01");//下季度的第一天DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();// 下季度最后一天DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day);// 上季度第一天DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).AddDays(1 - DateTime.Now.Day).AddDays(-1).ToShortDateString();// 上季度最后一天

 

原文:http://www.cnblogs.com/qinersky902/p/5065870.html(Tualatin)