SourceLogic Solutions Home Some extension methods for DateTime

Some extension methods for DateTime

by Craig 8/7/2008 3:11:00 PM

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.


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

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C#

Related posts

Comments

6/5/2009 9:16:36 PM

automated forex stock trading tools

Good one! that is why I am looking for . thank u

automated forex stock trading tools

11/20/2009 6:58:17 PM

fast payday loans

Thank you for your help!

fast payday loans us

12/4/2009 9:23:11 PM

burning calories

great and interesting post man, thanks a lot.

burning calories us

12/9/2009 10:58:48 PM

how to love love handles fast

I admire the informative post you share in your entries. I’ll bookmark your website and have my friends check up in your site frequently. I’m really sure they will learn lots of new information in your site than anybody else!

how to love love handles fast us

12/11/2009 11:03:41 PM

How to Become Taller

Thank you for providing great information regarding the topic. I’m a fan of your site. Keep up the great work.

How to Become Taller us

12/15/2009 6:32:41 PM

puzzle games

Hey admin, your blog's theme is so cool and loving it. Your posts are awesome. Please keep it up. Greets.

puzzle games

12/16/2009 6:35:58 AM

payday loans

I guess there's always an easier way ...

payday loans us

12/17/2009 3:38:27 PM

Maine Payday Loans

Interesting post

Maine Payday Loans us

Powered by BlogEngine.NET 1.3.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Pages

    Recent comments

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010

    Sign in