Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding first day of calendar

What i want to do is to create a simple calendar, and I want to find the first day of the first week of a specific month. My calendar is a Monday -> Sunday calendar and the following code works, but as you can see it's not that nice. Anyone have any better idea on how to get the first date in the calendar.

var now = new DateTime(Year, Month, 1);
now = now.AddDays(1-(int)now.DayOfWeek);
now = now.Day > 15 ? now : now.AddDays(-7);

The calendar will end up looking like this:

| <  |        Jan  2011       |  > |
------------------------------------
| Mo | Tu | We | Th | Fr | Sa | Su |
|[27]| 28 | 29 | 30 | 31 | 01 | 02 |
| 03 | 04 | 05 | 06 | 07 | 08 | 09 |
| .. | .. | .. | .. | .. | .. | .. |
| .. | .. | .. | .. | .. | .. | .. |
| 31 | 01 | 02 | 03 | 04 | 05 | 06 |

And in this "image" it's the [27] date that i'm trying to find.

Solution (Found i better/cleaner to loop then calculate):

    public DateTime FirstDay()
    {
        var date = new DateTime(Date.Year, Date.Month, 1);
        while (true)
        {
            if (date.DayOfWeek == DayOfWeek.Monday)
                return date;
            date = date.AddDays(-1);
        }
        return date;
    }

    public DateTime LastDay()
    {
        var date = new DateTime(Date.Year, Date.Month, 
                                DateTime.DaysInMonth(Date.Year, Date.Month));
        while (true)
        {
            if (date.DayOfWeek == DayOfWeek.Sunday)
                return date;
            date = date.AddDays(1);
        }
        return date;
    }

/BR Andreas

like image 644
Andreas Avatar asked Feb 03 '11 07:02

Andreas


People also ask

How do you determine the first day of the month?

We can use the EOMONTH formula to find the first day of the month as well. EOMONTH returns the last day of a month from a date. Here, we use the EOMONTH function to go to the last day of the previous month. Then, we add 1 to get the first day of the current month.

How do you determine the first day of the week?

To get the first day of the current week, get the day of the month, subtract the day of the week and add 1 to consider Monday the first day of the week.

What is the easiest way to find the day of a date?

Take the last 2 digits of the year. Divide it by 4 and discard any remainder. Add the day of the month to the value obtained in step 2.


1 Answers

I would just do this. It is so easy to understand:

var firstDayOfMonth = new DateTime(year, month, 1);
DateTime startOfCalendar = 
    FirstDayOfWeekOnOrBefore(
        firstDayOfMonth,
        DayOfWeek.Monday
    );

public static DateTime FirstDayOfWeekOnOrBefore(
    DateTime date,
    DayOfWeek dayOfWeek
) {
    while(date.DayOfWeek != dayOfWeek) {
        date = date.AddDays(-1);
    }
    return date;
}

Additionally, if you want to change your calendar to start on something other than Monday, it's trivial now. A solution using modulo arithmetic would not be as maintainable.

like image 98
jason Avatar answered Oct 12 '22 12:10

jason