Ejemplo n.º 1
0
        /// <summary>
        /// Gets the xth weekday
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static int Weekday(int month, int year, int count)
        {
            if (count != 5)
            {
                DateTime firstDay = new DateTime(year, month, 1);

                // Get the first weekday
                firstDay = JumpToWeekday(firstDay);

                // Add the number of days
                firstDay = firstDay.AddDays(count - 1);

                firstDay = JumpToWeekday(firstDay);

                return(firstDay.Day);
            }
            else
            {
                DateTime lastDay = new DateTime(year, month, CalendarHelpers.DaysInMonth(month, year));

                if (lastDay.DayOfWeek == System.DayOfWeek.Sunday)
                {
                    lastDay = lastDay.AddDays(-2);
                }
                else if (lastDay.DayOfWeek == System.DayOfWeek.Saturday)
                {
                    lastDay = lastDay.AddDays(-1);
                }

                return(lastDay.Day);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the xth weekend day
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static int WeekendDay(int month, int year, int count)
        {
            if (count != 5)
            {
                DateTime firstDay = new DateTime(year, month, 1);

                // Get the first weekday
                firstDay = JumpToWeekend(firstDay);

                // Add the number of days
                for (int i = 0; i < count - 1; i++)
                {
                    firstDay = firstDay.AddDays(1);
                    firstDay = JumpToWeekend(firstDay);
                }

                return(firstDay.Day);
            }
            else
            {
                DateTime lastDay = new DateTime(year, month, CalendarHelpers.DaysInMonth(month, year));

                while (lastDay.DayOfWeek != System.DayOfWeek.Saturday && lastDay.DayOfWeek != System.DayOfWeek.Sunday)
                {
                    lastDay = lastDay.AddDays(-1);
                }

                return(lastDay.Day);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the number of weeks in a month defined by a DateTime.
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static int NumberOfWeeks(DateTime date)
        {
            DateTime lastDay = new DateTime(date.Year, date.Month, CalendarHelpers.DaysInMonth(date.Month, date.Year));

            lastDay = lastDay.AddDays(-(lastDay.DayOfWeek - date.DayOfWeek));

            if (lastDay.Month != date.Month)
            {
                lastDay = lastDay.AddDays(-7);
            }

            DateTime firstDay = new DateTime(date.Year, date.Month, 1);

            firstDay = firstDay.AddDays(-(firstDay.DayOfWeek - date.DayOfWeek));

            if (firstDay.Month != date.Month)
            {
                firstDay = firstDay.AddDays(7);
            }

            return((lastDay.Day - firstDay.Day) / 7 + 1);
        }