VAAA
VAAA

Reputation: 15039

How to get the first and last day of a month calendar view (sunday-saturday)

I have a calendar that's first week day starts in Sunday and ends in Saturday.

enter image description here

Right now I can only disable days in the calendar current month because I don't know the first and last day in the calendar.

The code that Im using is pretty simple right now:

private List<DateTime> GetDisabledDates(DateTime fromDate, DateTime toDate){

// right now fromDate and toDate are the start and end days in a month

var disabledDates = SearchDates(fromDate, toDate);

return disabledDates;

}

So, what I need is to get the first day and last day showed in the calendar month, considering that week starts in Sunday and ends in Saturday.

enter image description here

Any clue on how to dinamically get first and last (yellow marked dates) from a specific month? Considering the calendar configuration?

Upvotes: 1

Views: 2732

Answers (3)

Jose Tepedino
Jose Tepedino

Reputation: 1584

Here´s my suggestion, defining year and month as parameters:

public DateTime[] GetMonthDisplayLimits(int year, int month)
{
    int lastDay = DateTime.DaysInMonth(year, month);
    var firstDayInMonth = new DateTime(year, month, 1);
    var lastDayInMonth = new DateTime(year, month, lastDay);

    var firstDayInView = firstDayInMonth.AddDays(-1 * (int) firstDayInMonth.DayOfWeek);
    var lastDayInView = lastDayInMonth.AddDays((int) (6 - lastDayInMonth.DayOfWeek));

    return new DateTime[] { firstDayInView, lastDayInView };
}

DateTime[] monthDisplayLimits = GetMonthDisplayLimits(2014, 8);

var firstDayInView = monthDisplayLimits[0];
var lastDayInView  = monthDisplayLimits[1];

Since "DayOfWeek" is a value between 0 and 6, this approach rounds down the first weekday and rounds up the last weekday.

Upvotes: 0

VAAA
VAAA

Reputation: 15039

The solution that works for me:

int totalCalendarDays = 42; // matrix 7 x 6

// set the first month day
DateTime firstDayMonth = new DateTime(date.Year, date.Month, 1);

 // set the lastmonth day
DateTime lastDayMonth = new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));

// now get the first day week of the first day month (0-6 Sun-Sat)
byte firstDayWeek = (byte) firstDayMonth.DayOfWeek;

// now get the first day week of the last day month (0-6 Sun-Sat)
byte lastDayWeek = (byte) lastDayMonth.DayOfWeek;

// now the first day show in calendar is the first day month minus the days to 0 (sunday)
DateTime firstDayCalendar = firstDayMonth.Subtract(TimeSpan.FromDays(firstDayWeek));
int tempDays = (lastDayMonth - firstDayCalendar).Days;

DateTime lastDayCalendar = lastDayMonth.Add(TimeSpan.FromDays(totalCalendarDays - tempDays - 1));

Maybe is a better way to do this :)

Upvotes: 1

Fionn
Fionn

Reputation: 11265

Well for the first day in this view something like this should do it

//Using UTC to counter daylight saving problems
var month = new DateTime(2014, 8, 1, 0, 0, 0, DateTimeKind.Utc); 
var firstInView = month.Subtract(TimeSpan.FromDays((int) month.DayOfWeek));

For the remaining days you just need to calculate the amount left in (7 * NumRows) - (DaysOfCurrentMonth + DaysOfPreviousMonth), where DaysOfPreviousMonth is the DayOfWeek property of this month first day again.

Upvotes: 1

Related Questions