.net获取年初,年末。从一年的第一天输出到年末。从1月1号开始输出到12月31号。从一年的第一天输出到当然日期。输出1到12月。月初时间和月末时间。时间日期相关。某个月最后一天。本周周一与周日。本周,本月,每周,每月 电脑版发表于:2020/11/19 18:29 [TOC] ## 获取年初,年末,从一年的第一天输出到年末 ### 方法1 ``` DateTime startYear = new DateTime(DateTime.Now.Year, 1, 1); //本年年初 DateTime endYear = new DateTime(DateTime.Now.Year, 12, 31); //本年年末 //获取一年的天数 int days = (DateTime.IsLeapYear(DateTime.Now.Year) ? 366 : 365); //从一年的第一天输出到年末 for (int i = 0; i < days; i++) { Console.WriteLine(startYear.Date.AddDays(i).ToShortDateString()); } ``` **效果如下** 从1月1号开始 ![](https://img.tnblog.net/arcimg/aojiancc2/d06f71edc7ae4f6b8a090a28ef4252eb.png) 输出到12月31号 ![](https://img.tnblog.net/arcimg/aojiancc2/b0ba97f610d0408882c76a67b52e3b6b.png) ### 方法2: ``` DateTime startYear = new DateTime(DateTime.Now.Year, 1, 1); //本年年初 DateTime endYear = new DateTime(DateTime.Now.Year, 12, 31); //本年年末 //通过日期差来计算天数 double days = (endYear - startYear).TotalDays; Console.WriteLine(days); //从一年的第一天输出到年末 for (int i = 0; i <= days; i++) { Console.WriteLine(startYear.Date.AddDays(i).ToShortDateString()); } ``` ## 从一年的第一天输出到当前日期 ``` DateTime startYear = new DateTime(DateTime.Now.Year, 1, 1); //本年年初 //通过日期差来计算天数 double days = (DateTime.Now - startYear).TotalDays; Console.WriteLine(days); //从一年的第一天输出到当然日期 for (int i = 0; i <= days; i++) { Console.WriteLine(startYear.Date.AddDays(i).ToShortDateString()); } ``` ## 输出1到12月 ``` DateTime startYear = new DateTime(DateTime.Now.Year, 1, 1); //本年年初 for (int i = 0; i < 12; i++) { Console.WriteLine(startYear.AddMonths(i).ToString("yyyy-MM-dd")); } ``` ![](https://img.tnblog.net/arcimg/aojiancc2/b825e295ecfe4319aa942e40918ffef5.png) ## 根据传过来的月份,计算这个月最后一天 ``` /// <summary> /// 根据传过来的月份,计算这个月最后一天 /// </summary> /// <param name="month"></param> /// <returns></returns> public DateTime GetMonthEndTime(DateTime month) { //本月第一天时间 DateTime stime = month.AddDays(1 - (month.Day)); //获得某年某月的天数 int y = month.Date.Year; int m = month.Date.Month; int dayCount = DateTime.DaysInMonth(y, m); DateTime endtime = stime.AddDays(dayCount - 1); return endtime; } ``` ## 输出某月的月初时间和月末时间 **封装一个方法** ``` /// <summary> /// 根据时间点计算当月开始时间和结束时间 /// </summary> /// <param name="date"></param> /// <returns></returns> public static DateTime[] GetMonthStatTimeAndEndTime(DateTime date) { //你可能需要处理一下时区 //date = date.ToCstTime(); //本月第一天时间 DateTime stime = date.AddDays(1 - (date.Day)); //获得某年某月的天数 int y = date.Date.Year; int m = date.Date.Month; int dayCount = DateTime.DaysInMonth(y, m); DateTime etime = stime.AddDays(dayCount - 1); return new List<DateTime>() { stime, etime }.ToArray(); //return new List<DateTime>() { DateTime.Parse("2000-1-1"), DateTime.Parse("2022-4-9") }.ToArray(); } ``` 调用: ![](https://img.tnblog.net/arcimg/aojiancc2/2d542a6f9e4348328d65914a077afc2d.png) ## 获取从1月至12月的月初时间和月末时间 ``` DateTime startYear = new DateTime(DateTime.Now.Year, 1, 1); //本年年初 for (int i = 0; i < 12; i++) { DateTime[] dateTimes = DateTimeTools.GetMonthStatTimeAndEndTime(startYear.AddMonths(i)); Console.WriteLine(dateTimes[0].ToString("yyyy-MM-dd") + "--" + dateTimes[1].ToString("yyyy-MM-dd")); } ``` ![](https://img.tnblog.net/arcimg/aojiancc2/898bfd02da574df596d99ebb72ca0f4a.png) ## 获取从1月至当前月的月初时间和月末时间 ``` DateTime startYear = new DateTime(DateTime.Now.Year, 1, 1); //本年年初 //获取当前月 int month = DateTime.Now.Month; for (int i = 0; i < month; i++) { DateTime[] dateTimes = DateTimeTools.GetMonthStatTimeAndEndTime(startYear.AddMonths(i)); Console.WriteLine(dateTimes[0].ToString("yyyy-MM-dd") + "--" + dateTimes[1].ToString("yyyy-MM-dd")); } ``` ![](https://img.tnblog.net/arcimg/aojiancc2/97a2f5909ca446c58a2203279fb06b0a.png) ## C#获取本周的周一以及周日 ``` /// <summary> /// C#获取本周的周一以及周日 /// </summary> /// <returns></returns> public static DateTime[] GetWeekStartAndEnd() { DateTime dt = DateTime.Now; int dayOfWeek = -1 * (int)dt.Date.DayOfWeek; //Sunday = 0,Monday = 1,Tuesday = 2,Wednesday = 3,Thursday = 4,Friday = 5,Saturday = 6, DateTime weekStartTime = dt.AddDays(dayOfWeek + 1);//取本周一 if (dayOfWeek == 0) //如果今天是周日,则开始时间是上周一 { weekStartTime = weekStartTime.AddDays(-7); } return new List<DateTime>() { weekStartTime.Date, weekStartTime.Date.AddDays(6) }.ToArray(); } ``` ## 按本日,本周,本月的时间范围查询 ``` DateTime beginDate = DateTime.Now.Date; DateTime endDate = _params.dateTime.Date.AddDays(1); // 按天统计 if (_params.Type == "dateParamDay") { endDate = beginDate.Date.AddDays(1); } // 按周统计 if (_params.Type == "dateParamWeek") { int dayOfWeek = -1 * (int)beginDate.Date.DayOfWeek; DateTime weekStartTime = beginDate.AddDays(dayOfWeek + 1);//取本周一 if (dayOfWeek == 0) //如果今天是周日,则开始时间是上周一 { weekStartTime = weekStartTime.AddDays(-7); } beginDate = weekStartTime; endDate = weekStartTime.Date.AddDays(7); } // 按月统计 if (_params.Type == "dateParamMonth") { //本月第一天时间 DateTime stime = beginDate.AddDays(1 - (beginDate.Day)); beginDate = stime; endDate = beginDate.AddMonths(1); } // 根据选择的时间范围查询 List<StuDailyAssignment> stuDailyAssignments = BaseDal.Db.Queryable<StuDailyAssignment>().Where(a => studentIDList.Contains(a.StuID) && a.DAStatus != 1 && a.CreateTime >= beginDate && a.CreateTime < endDate).ToList(); ```