首页 > 代码库 > 根据周几计算出月份总年中所有此日的日期值

根据周几计算出月份总年中所有此日的日期值

/// <summary>        /// 根据年月获取所有周几        /// </summary>        /// <param name="y"></param>        /// <param name="m">月份字符串</param>        /// <param name="week">周几字符串(0:—....6:日)</param>        /// <returns>时间列表</returns>        public static List<string> GetAllWeek(int y, string m, string week)        {            List<string> dt = new List<string>();          // 用来记录每月周几存在列表            string[] monthsArr = m.Substring(0, m.Length - 1).Split(,);  //获取每月            foreach (string monthStr in monthsArr)            {                if (!string.IsNullOrEmpty(monthStr))                {                    int month = Convert.ToInt32(monthStr);                    int Month_number = DateTime.DaysInMonth(y, month);          // 用来记录一个月中的天数                         for (int i = 1; i <= Month_number; i++)                    {                        string[] weektimeArr = week.Substring(0, week.Length - 1).Split(_);  //获取周几与时间                        foreach (string wt in weektimeArr)                        {                            string w = wt.Split()[0];                            if (!string.IsNullOrEmpty(w))                            {                                if (Common.Whether_Weekend(y, month, i, Convert.ToInt32(w)))                                {                                    dt.Add(w + "" + month + "-" + i + "" + wt.Split()[1]);                                }                            }                        }                    }                }            }            return dt;        }        /// <summary>        /// 根据年某月月获取所有周几        /// </summary>        /// <param name="y"></param>        /// <param name="m"></param>        /// <param name="week">周几(0:—....6:日)</param>        /// <returns>时间列表</returns>        public static List<DateTime> GetAllWeek(int y, int m, int week)        {            List<DateTime> dt = new List<DateTime>();          // 用来记录这月周几存在列表            int Month_number = DateTime.DaysInMonth(y, m);     // 用来记录一个月中的天数                 for (int i = 1; i <= Month_number; i++)            {                if (Common.Whether_Weekend(y, m, i, week))                { dt.Add(Convert.ToDateTime(m + "-" + i)); }            }            return dt;        }        /// <summary>        /// 计算是周几是否存在于某月        /// </summary>        /// <param name="y"></param>        /// <param name="m"></param>        /// <param name="d">天数</param>        /// <param name="needdate">周几(0:—....6:日)</param>        /// <returns>是否存在</returns>        public static bool Whether_Weekend(int y, int m, int d, int needdate)        {            if (m == 1 || m == 2)            {                m += 12;                y--;            }            int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;   // 基姆拉尔森公式                 if (week == needdate)            {                return true;            }            else            {                return false;            }        }