码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-04-22 16:23:36      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:

        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

 

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

标签:

原文地址:http://www.cnblogs.com/zhuiyi/p/5421300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!