Date Range in C#

Get Date in Week:

public static DateTime EndOfWeek(DateTime dateTime)
    {
        DateTime start = StartOfWeek(dateTime);

        return start.AddDays(6);
    }

    public static DateTime StartOfWeek(DateTime dateTime)
    {
        int days = dateTime.DayOfWeek - DayOfWeek.Monday; 

        if (days < 0) 
            days += 7;

        return dateTime.AddDays(-1 * days).Date;
    }

Get Date in Month:

public DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
{
   return new DateTime(dateTime.Year, dateTime.Month, 1);
}
public DateTime LastDayOfMonthFromDateTime(DateTime dateTime)
{
   DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
   return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
}

Referenced by: http://stackoverflow.com/questions/5080152/find-week-ending-date-of-last-completed-week
http://www.devprise.com/2006/10/03/getting-the-first-and-last-day-of-the-month-with-net-c/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s