Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

Blog Directory
Blogging Fusion Blog Directory
Web Directory
Blog Directory
EatonWeb Blog Directory
GeekySpeaky: Submit Your Site!
Blog Directory
blogarama - the blog directory
Bloglisting.net - The internets fastest growing blog directory
Blogio.net blog directory
Free Blog Directory
blog search directory
Software Blogs
RSSMicro FeedRank Results
On our way to 1,000,000 rss feeds - millionrss.com
Listed in LS Blogs the Blog Directory and Blog Search Engine
blog directory
Link With Us - Web Directory
Web Blogs Directory Add Your Blog.com

Certificates

Links

Social

Some extension methods for DateTime

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! :)

Posted: Jul 16 2008, 10:20 PM by DigiMortal | with 10 comment(s)
Filed under:

Comments

Peter said:

"(-1)*currentDate.Day" no need for all that, just use "-currentDate.Day"

# July 16, 2008 4:18 PM

DigiMortal said:

Thanks for suggestion! :)

# July 16, 2008 4:21 PM

Patrik Hägne said:

... (-1)*currentDate.Day + 1 ...

That must be the hardest way to say "1 - currentDate.Day" ever.

# July 16, 2008 5:27 PM

DigiMortal said:

Patrik, I am sure that in my blondest moments I am able to find harder way than the one you pointed out :)

# July 17, 2008 1:43 AM

Dew Drop - July 17, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - July 17, 2008 | Alvin Ashcraft's Morning Dew

# July 17, 2008 12:29 PM

Patrik Hägne said:

Haha, I respect you for that!

# July 17, 2008 6:34 PM

DigiMortal said:

Thanks, man! :D

# July 18, 2008 1:29 AM

Christopher Steen said:

AJAX ASP .net AJAX Event handlers - the very very basics [Via: xxxd ] AJAX: Working with Events [Via:...

# July 18, 2008 8:15 AM

Christopher Steen said:

Link Listing - July 17, 2008

# July 18, 2008 8:16 AM

John said:

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);

# July 30, 2008 10:52 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)