コード例 #1
0
        public static MonthWeeks NumWeeks(this DateTime dt)
        {
            var monthWeeks = new MonthWeeks();

            monthWeeks.WeekList = new List <MonthWeekStartEnd>();

            //年
            var year = dt.Year;
            //月
            var month = dt.Month;
            //当前月第一天
            var weekStart = new DateTime(year, month, 1);
            //该月的最后一天
            var monEnd = weekStart.AddMonths(1).AddDays(-1);
            var i      = 1;
            //当前月第一天是星期几
            var dayOfWeek = Convert.ToInt32(weekStart.DayOfWeek.ToString("d"));
            //该月第一周结束日期
            var weekEnd = dayOfWeek == 6 ? weekStart : weekStart.AddDays(6 - dayOfWeek);

            monthWeeks.WeekList.Add(new MonthWeekStartEnd()
            {
                WeekStart = weekStart,
                WeekEnd   = weekEnd.AddDays(1).AddSeconds(-1)
            });

            //当日期小于或等于该月的最后一天
            while (weekEnd.AddDays(1) <= monEnd)
            {
                i++;
                //该周的开始时间
                weekStart = weekEnd.AddDays(1);
                //该周结束时间
                weekEnd = weekEnd.AddDays(7) > monEnd ? monEnd : weekEnd.AddDays(7);


                monthWeeks.WeekList.Add(new MonthWeekStartEnd()
                {
                    WeekStart = weekStart,
                    WeekEnd   = weekEnd.AddDays(1).AddSeconds(-1)
                });
            }

            monthWeeks.WeekCount = i;

            return(monthWeeks);
        }
コード例 #2
0
ファイル: StringExtensions.cs プロジェクト: b9502032/MySite
        public static MonthWeeks NumWeeks(this DateTime dt)
        {
            var monthWeeks = new MonthWeeks();
            monthWeeks.WeekList = new List<MonthWeekStartEnd>();

            //年
            var year = dt.Year;
            //月
            var month = dt.Month;
            //当前月第一天
            var weekStart = new DateTime(year, month, 1);
            //该月的最后一天
            var monEnd = weekStart.AddMonths(1).AddDays(-1);
            var i = 1;
            //当前月第一天是星期几
            var dayOfWeek = Convert.ToInt32(weekStart.DayOfWeek.ToString("d"));
            //该月第一周结束日期
            var weekEnd = dayOfWeek == 6 ? weekStart : weekStart.AddDays(6 - dayOfWeek);

            monthWeeks.WeekList.Add(new MonthWeekStartEnd()
                {
                    WeekStart = weekStart,
                    WeekEnd = weekEnd.AddDays(1).AddSeconds(-1)
                });

            //当日期小于或等于该月的最后一天
            while (weekEnd.AddDays(1) <= monEnd)
            {
                i++;
                //该周的开始时间
                weekStart = weekEnd.AddDays(1);
                //该周结束时间
                weekEnd = weekEnd.AddDays(7) > monEnd ? monEnd : weekEnd.AddDays(7);

                monthWeeks.WeekList.Add(new MonthWeekStartEnd()
                {
                    WeekStart = weekStart,
                    WeekEnd = weekEnd.AddDays(1).AddSeconds(-1)
                });
            }

            monthWeeks.WeekCount = i;

            return monthWeeks;
        }