Пример #1
0
        /// <summary>
        /// 根据一年中的第几周获取该周的开始日期与结束日期
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="weekNumber">周:> 0 的正整数</param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public static Tuple <DateTime, DateTime> GetFirstEndDayOfWeek(int year, int weekNumber)
        {
            System.Globalization.Calendar calendar = System.Globalization.CultureInfo.CurrentCulture.Calendar;
            DateTime  firstOfYear    = new DateTime(year, 1, 1, calendar);
            DateTime  targetDay      = calendar.AddWeeks(firstOfYear, weekNumber - 1);
            DayOfWeek firstDayOfWeek = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;

            while (targetDay.DayOfWeek != firstDayOfWeek)
            {
                targetDay = targetDay.AddDays(-1);
            }

            return(Tuple.Create <DateTime, DateTime>(targetDay, targetDay.AddDays(6)));
        }
Пример #2
0
        /// <summary>
        /// Creates List[Chunk], with regular start and end dates for required intervals.  Amount = 0 for all Chunks.
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="interval"></param>
        /// <returns>List[Chunk] for required duration, each Chunk covering one interval.</returns>
        public List <Chunk> setupDatedChunksForApportionToPeriod(DateTime startDate, DateTime endDate, AppotionmentPeriod interval)
        {
            ///calendar object used for adding days/months/years to start date
            System.Globalization.Calendar calendar = System.Globalization.CultureInfo.CurrentCulture.Calendar;

            ///set up apportionment requirements
            List <Chunk> datedChunks = new List <Chunk>();
            int          periodType  = (int)interval;

            ///i used to keep track of how many Chunks have already been created & adds appropriate no. of intervals to start date.
            int i = 0;

            ///create list of chunks with correct required start dates
            do
            {
                Chunk    chunk  = new Chunk();
                TimeSpan oneDay = new TimeSpan(1, 0, 0); ///one day timespan needed to calculate end dates

                switch (periodType)
                {
                case 1:     ///interval is daily
                    ///

                    chunk.StartDate = calendar.AddDays(startDate, i);
                    chunk.EndDate   = calendar.AddDays(chunk.StartDate, 1);
                    break;

                case 2:     ///interval is weekly
                    ///
                    chunk.StartDate = calendar.AddWeeks(startDate, i);
                    chunk.EndDate   = calendar.AddWeeks(chunk.StartDate, 1);
                    break;

                case 3:     ///interval is monthly
                    ///
                    chunk.StartDate = calendar.AddMonths(startDate, i);
                    chunk.EndDate   = calendar.AddMonths(chunk.StartDate, 1);
                    break;

                case 4:     ///interval is quarterly
                    ///
                    chunk.StartDate = calendar.AddMonths(startDate, 3 * i);
                    chunk.EndDate   = calendar.AddMonths(chunk.StartDate, 3 * 1);

                    break;

                case 5:     ///interval is annually
                    ///
                    chunk.StartDate = calendar.AddYears(startDate, i);
                    chunk.EndDate   = calendar.AddYears(chunk.StartDate, 1);
                    break;
                }

                ///take on day off each date date to avoid overlapping date ranges
                chunk.EndDate = chunk.EndDate.Subtract(oneDay);
                datedChunks.Add(chunk);
                i++;
            } while (datedChunks.ElementAt(i - 1).EndDate <= endDate);

            return(datedChunks);
        }