Nifty Extension Methods For DateTime in C#

Aztec

These are some easy and quick date extensions which will make your life easier with dates. You can copy these extension methods in a CS file in your project and Intellisense will be more than happy to join these methods in the armory of its DateTime methods.

Intellisense

Let’s get to coding.

Have a CS file in your project containing code like so:

using System;
using System.Threading;
using MyProject.Extensions;

namespace MyProject.Extensions
{
    public static class DateExtensions
    {
    }
}

We will write all our static extension methods inside this DateExtensions class.

Start Date of The Week

You wish to get start date of the week to which the date belongs. You also want that the date returned should adhere to the culture of the device/thread because different cultures have different first day of week. In some cultures Monday is the first day of the week, in some Sunday, and in some Friday. Usage scenario where you might want to use this method is – Sub-total of some value in the duration of the week of the give date or current date.  Here is the method:

        public static DateTime StartDateOfTheWeek(this DateTime dt)
        {
            DateTime _returnDateTime = dt.AddDays(-((dt.DayOfWeek - Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek)));
            return _returnDateTime;
        }

Call this method like so:

            DateTime _dt = new DateTime();
            DateTime _weekStart = _dt.StartDateOfTheWeek();

End Date of The Week

This is going to be simple. We will get first date of the week, from the method above, and add 6 days.

        public static DateTime EndDateOfTheWeek(this DateTime dt)
        {
            return dt.StartDateOfTheWeek().AddDays(6);
        }

Calling this method is simple:

            DateTime _dt = new DateTime();
            DateTime _weekEnd = _dt.EndDateOfTheWeek();

Start Date of The Month

Getting start date of the month is simple:

        public static DateTime StartDateOfTheMonth(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1);
        }

End Date of The Month

Getting end date of the month requires a little tweaking. We will add number of days in the month to the start date and then subtract one day to find the end date of the month.

        public static DateTime EndDateOfCurrentMonth(this DateTime dt)
        {
            return DateTime.Now.StartDateOfTheMonth().AddDays(DateTime.DaysInMonth(dt.Year, dt.Month) - 1);
        }

Following is the complete listing of the code. You can copy/paste this code in a CS file in your project, compile the code, and have your DateTime object call these methods.

using System;
using System.Threading;
using MyProject.Extensions;

namespace MyProject.Extensions
{
    public static class DateExtensions
    {
        public static DateTime StartDateOfTheWeek(this DateTime dt)
        {
            DateTime _returnDateTime = dt.AddDays(-((dt.DayOfWeek - Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek)));
            return _returnDateTime;
        }
        public static DateTime EndDateOfCurrentWeek(this DateTime dt)
        {
            return dt.StartDateOfTheWeek().AddDays(6);
        }
        public static DateTime StartDateOfTheMonth(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1);
        }
        public static DateTime EndDateOfCurrentMonth(this DateTime dt)
        {
            return DateTime.Now.StartDateOfTheMonth().AddDays(DateTime.DaysInMonth(dt.Year, dt.Month) - 1);
        }
    }
}

Happy Date-ing! 😀

Advertisements

5 thoughts on “Nifty Extension Methods For DateTime in C#

  1. Pingback: Auto-increment Builds – Part 1 | Sharp Statements

  2. Pingback: C# | .NET Extension Methods | Sharp Statements

  3. Pingback: DateTime .NET and SQL Server « El TecnoBaúl de Kiquenet

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s