/// <summary> /// Creates a data range for the year, month, day supplied. /// </summary> /// <param name="year"></param> /// <param name="month"></param> /// <param name="day"></param> /// <returns></returns> public static DateRange CreateForMonth(int year, int month, int day) { //var currentTime = new DateTime(year, month, 1); DateTime refDate = new DateTime(year, month, 1); DateRangeMonth range = new DateRangeMonth(); DateTime rangeStartDate = refDate; // 1. Current month // ( Total days, start date, start day indx ) range.TotalDays = DateHelper.GetDaysInMonth(month, year); range.CurrentMonthStartDate = refDate; range.CurrentMonthStartIndex = (int)range.CurrentMonthStartDate.DayOfWeek; // 2. Last month DateTime lastMonth = refDate.AddMonths(-1); int daysToShowInLastMonth = range.CurrentMonthStartIndex; range.TotalDaysInLastMonth = DateHelper.GetDaysInMonth(lastMonth.Month, lastMonth.Year); if (daysToShowInLastMonth == 0) { rangeStartDate = range.CurrentMonthStartDate; range.LastMonthStartDate = DateTime.MinValue; } else { int startDay = range.TotalDaysInLastMonth - (daysToShowInLastMonth - 1); range.LastMonthStartDate = new DateTime(lastMonth.Year, lastMonth.Month, startDay); rangeStartDate = range.LastMonthStartDate; } // 3. Current day index range.CurrentDayIndex = day + daysToShowInLastMonth - 1; // 4. Next month. DateTime nextMonth = refDate.AddMonths(1); int daysToShowInNextMonth = DateRangeMonth.TotalDaysVisible - (range.TotalDays + range.CurrentMonthStartIndex); range.TotalDaysVisibleInNextMonth = daysToShowInNextMonth; range.NextMonthStartIndex = DateRangeMonth.TotalDaysVisible - (daysToShowInNextMonth); // 5. Now get the dates CreateForXDays(range, DateRangeMonth.TotalDaysVisible, rangeStartDate, DateTime.Now); return range; }
/// <summary> /// Creates a range for the month taking into account the start day of the week. /// </summary> /// <param name="year"></param> /// <param name="month"></param> /// <param name="day"></param> /// <param name="startDayOfWeek"></param> /// <returns></returns> public static DateRange CreateForMonth(int year, int month, int day, string startDayOfWeek) { //var currentTime = new DateTime(year, month, 1); DateTime refDate = new DateTime(year, month, 1); DateTime currentMonthStartDate = refDate; DateRangeMonth range = new DateRangeMonth(); // 1. Get the start day. WeekRangeCalculator calc = new WeekRangeCalculator(startDayOfWeek); DateTime rangeStartDate = calc.GetStartDayForDate(refDate); // 2. Current month range.CurrentMonthTotalDays = DateHelper.GetDaysInMonth(month, year); range.CurrentMonthStartDate = currentMonthStartDate; range.CurrentMonthStartIndex = calc.GetDayValue(refDate.DayOfWeek); // 3. Last month DateTime lastMonth = refDate.AddMonths(-1); range.LastMonthTotalDays = DateHelper.GetDaysInMonth(lastMonth.Month, lastMonth.Year); range.LastMonthStartDate = range.CurrentMonthStartIndex == 0 ? DateTime.MinValue : rangeStartDate; range.LastMonthStartIndex = range.CurrentMonthStartIndex == 0 ? -1 : 0; range.LastMonthDaysVisible = range.CurrentMonthStartIndex == 0 ? 0 : range.CurrentMonthStartIndex; // 4. Next month DateTime nextMonth = refDate.AddMonths(1); range.NextMonthTotalDays = DateHelper.GetDaysInMonth(lastMonth.Month, lastMonth.Year); range.NextMonthStartIndex = range.LastMonthDaysVisible + range.CurrentMonthTotalDays; range.NextMonthStartDate = nextMonth; // 5. Current day. range.CurrentDayIndex = day + range.LastMonthDaysVisible - 1; CreateForXDays(range, DateRangeMonth.TotalDaysVisible, rangeStartDate, DateTime.Now); return range; }
/// <summary> /// Create the range for month supplied. /// </summary> /// <param name="month"></param> /// <returns></returns> public static DateRange CreateForPriorMonth(DateRangeMonth range) { DateTime month = range.CurrentMonthStartDate.AddMonths(-1); int day = GetDayForMonth(month); return CreateForMonth(month.Year, month.Month, day); }