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));spa

輸出:3code

相關文章
相關標籤/搜索