c#计算某年某月的第几周所在日期

如:2013年12月第一周(2013-12-02 ~ 2013-12-08)
2013年12月第二周(2013-12-09 ~ 2013-12-15)
2013年12月第三周(2013-12-16 ~ 2013-12-22)
2013年12月第四周(2013-12-23 ~ 2013-12-29)
2013年12月第五周(2013-12-30 ~ 2014-01-05)
有些月份会有4个周,有些会有5个周。

 public List<string> GetData(int year,int month)
        {
            List<string> list = new List<string>();
            DateTime date = new DateTime(year, month, 1);
            int firstWeekDate = 1;
            for (int i = 1; i <= date.AddMonths(1).AddDays(-1).Day; i++)
            {
                if (new DateTime(date.Year, date.Month, i).DayOfWeek == DayOfWeek.Monday)
                {
                    firstWeekDate = i;
                    string temp = new DateTime(year, month, i).ToString("yyyy-MM-dd") + "~" + new DateTime(year, month, i).AddDays(6).ToString("yyyy-MM-dd");
                    list.Add(temp);
                }
            }
            return list;
        }

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2020-12-07
方法:
一种方法是自己写一个计算闰年的算法,并给定一个初始日期的星期。按照每个月的天数进行计算即可
另一种方法给定每一年的1月1日星期,这一年的天数,计算即可。
相似回答