首页 > 代码库 > 根据时间段和周几查询包含的周几对应的日期

根据时间段和周几查询包含的周几对应的日期

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace weekDemo{    class Program    {        static void Main(string[] args)        {            DayOfWeek[] dw = new DayOfWeek[] { DayOfWeek.Monday, DayOfWeek .Sunday};            var query = GetWeekDays(DateTime.Now, DateTime.Now.AddMonths(1), dw);            foreach (var item in query.ToList())            {                Console.WriteLine(item.ToShortDateString() + "--星期:" + System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(item.DayOfWeek));            }                   }        public static IEnumerable<DateTime> GetWeekDays(DateTime begin, DateTime end,DayOfWeek[] dws)        {            return Enumerable.Range(0, (end - begin).Days).Where(c => dws.Contains(begin.AddDays(c).DayOfWeek)).Select(c => begin.AddDays(c));        }    }}

 

根据时间段和周几查询包含的周几对应的日期