C# 获取当前日期在指定日期范围内是第几周

        public static int GetWeekOfDay(DateTime start, DateTime end)
        {
            //总周数
            var weekCount = Math.Ceiling(Convert.ToDouble((end - start).Days / 7));
            //用于存储日期
            var weekDic = new Dictionary<DateTime, int>();

            var today = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));

            for (var i = 0; i < weekCount; i++)
            {
                for (var j = 0; j < 7; j++)
                {
                    weekDic.Add(start.AddDays(i * 7 + j), i + 1);
                }
            }

            return weekDic.ContainsKey(today) ? weekDic[today] : 1;
        }

 调用: var result = DateHelper.GetWeekOfDay(new DateTime(2016, 04, 04), new DateTime(2016, 04, 25));

输出:3

 

posted @ 2016-04-22 14:51  ゞ追忆o0ゞ  阅读(2025)  评论(1编辑  收藏  举报