During one of my current projects I wrote some useful extension methods for DateTime class. I had to write custom calendar component and therefore I needed manipulate dates a little bit. Here is some extensions I would like to share with you. And, of course, comments and suggestions are welcome as always.
Why chose extension methods? Because there is always current date given for calendar. Even when activities in calendar are based on months there is always current date. So I wrote those methods in current state context.
Get first date of current month
public static DateTime GetFirstDateOfMonth(this DateTime currentDate)
{
return currentDate.AddDays((-1) * currentDate.Day + 1);
}
Get last date of current month
public static DateTime GetLastDateOfMonth(this DateTime currentDate)
{
return currentDate.AddMonths(1).GetFirstDateOfMonth().AddDays(-1);
}
Get dates array from current date to given date
I think that for common use it is better to make this method as static method of some helper or utility class. Then it more clear that one can use it with any date range. Also it is possible to create one overload for this class that accepts TimeSpan as argument.
public static DateTime[] GetDatesArray(this DateTime fromDate, DateTime toDate)
{
int days = (toDate - fromDate).Days;
var dates = new DateTime[days];
for (int i = 0; i < days; i++)
{
dates[i] = fromDate.AddDays(i);
}
return dates;
}
Get all date array for current calendar view
This method creates array that contains all dates in current month’s calendar view. If first day of month is tuesday then last day from previous month is also added to array to fill up the first monday in calendar. If last day of month is friday then two days from next month are also added to array to fill saturday and sunday slot.
NB! This method is very raw and it is not completely tested yet. So I cannot be 100% sure if i contains errors or not. The nasty hack here is changing week day number to make monday as first day of week (sorry, I live in this kind of region).
public static DateTime[] GetCalendarMonthDatesArray(this DateTime currentDate)
{
var first = currentDate.GetFirstDateOfMonth();
var firstWeekDay = (int)first.DayOfWeek;
if (firstWeekDay == 0)
firstWeekDay = 7;
var from = first.AddDays((int)DayOfWeek.Monday - firstWeekDay);
var last = currentDate.GetLastDateOfMonth();
var lastWeekDay = (int)last.DayOfWeek;
if (lastWeekDay == 0)
lastWeekDay = 7;
var thru = last.AddDays(lastWeekDay - (int)DayOfWeek.Monday + 1);
return from.GetDatesArray(thru);
}
So, what else I have to say? Happy extending! :)
View Comments (7)
"(-1)*currentDate.Day" no need for all that, just use "-currentDate.Day"
Thanks for suggestion! :)
... (-1)*currentDate.Day + 1 ...
That must be the hardest way to say "1 - currentDate.Day" ever.
Patrik, I am sure that in my blondest moments I am able to find harder way than the one you pointed out :)
Haha, I respect you for that!
Thanks, man! :D
Thanks for the share.
just a remark, the first method seems very complicated to me , i use to do that instead:
return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);