Sunday, October 28, 2018

DateTime extensions, Part I

When working on code from business world, often you need to get some calendar edges from a given point of time. Instead to write much code, I created some extension methods for short access on the DateTime data type. The time units (year, month, week, day) represents portions from the western, Gregorian calendar (don't use it, if you need to follow other calendar systems).

And here is the code:


using System;

namespace Luegisdorf.Extenders
{
    /// <summary>
    /// Provides extended functionality for <c>DateTime</c> type.
    /// All calculation are based on ticks as smallest date time part unit.
    /// All time units represents portions of the western, Gregorian calendar.
    /// </summary>
    public static class DateTimeExtender
    {

        /// <summary>
        /// Calculates the begin of day for a given date time
        /// </summary>
        /// <param name="dateTime">
        /// The date time base for calculation
        /// </param>
        /// <returns>
        /// The calculated begin of day
        /// </returns>
        public static DateTime BeginOfDay(this DateTime dateTime)
        {
            return dateTime.Date;
        }

        /// <summary>
        /// Calculates the begin of week for a given date time.
        /// The first day of week is sunday.
        /// </summary>
        /// <param name="dateTime">
        /// The date time base for calculation
        /// </param>
        /// <returns>
        /// The calculated begin of week
        /// </returns>
        public static DateTime BeginOfWeek(this DateTime dateTime)
        {
            return dateTime.AddDays(-(int)dateTime.DayOfWeek).BeginOfDay();
        }

        /// <summary>
        /// Calculates the begin of month for a given date time
        /// </summary>
        /// <param name="dateTime">
        /// The date time base for calculation
        /// </param>
        /// <returns>
        /// The calculated begin of month
        /// </returns>
        public static DateTime BeginOfMonth(this DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, 1, 0, 0, 0,
                dateTime.Kind);
        }

        /// <summary>
        /// Calculates the begin of year for a given date time
        /// </summary>
        /// <param name="dateTime">
        /// The date time base for calculation
        /// </param>
        /// <returns>
        /// The calculated begin of year
        /// </returns>
        public static DateTime BeginOfYear(this DateTime dateTime)
        {
            return new DateTime(dateTime.Year, 1, 1, 0, 0, 0, dateTime.Kind);
        }


        /// <summary>
        /// Calculates the end of year for a given date time
        /// </summary>
        /// <param name="dateTime">
        /// The date time base for calculation
        /// </param>
        /// <returns>
        /// The calculated end of month
        /// </returns>
        public static DateTime EndOfYear(this DateTime dateTime)
        {
            return new DateTime(dateTime.Year + 1, 1, 1, 0, 0, 0,
                dateTime.Kind).AddTicks(-1);
        }


        /// <summary>
        /// Calculates the end of month for a given date time
        /// </summary>
        /// <param name="dateTime">
        /// The date time base for calculation
        /// </param>
        /// <returns>
        /// The calculated begin of year
        /// </returns>
        public static DateTime EndOfMonth(this DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month,
                DateTime.DaysInMonth(dateTime.Year, dateTime.Month), 0, 0, 0,
                    dateTime.Kind).AddDays(1).AddTicks(-1);
        }

        /// <summary>
        /// Calculates the end of week for a given date time.
        /// The last day of week is saturday.
        /// </summary>
        /// <param name="dateTime">
        /// The date time base for calculation
        /// </param>
        /// <returns>
        /// The calculated end of week
        /// </returns>
        public static DateTime EndOfWeek(this DateTime dateTime)
        {
            return dateTime.AddDays(Enum.GetValues(typeof(DayOfWeek)).Length
                - (int)dateTime.DayOfWeek - 1).EndOfDay();
        }

        /// <summary>
        /// Calculates the end of day for a given date time
        /// </summary>
        /// <param name="dateTime">
        /// The date time base for calculation
        /// </param>
        /// <returns>
        /// The calculated end of day
        /// </returns>
        public static DateTime EndOfDay(this DateTime dateTime)
        {
            return dateTime.Date.AddDays(1).AddTicks(-1);
        }
    }
}

And you can use it like this:


using System;
using Luegisdorf.Extenders;

namespace Luegisdorf.Example
{
    class SampleClass
    {
        public void DoSomething()
        {
            var anyDate = new DateTime(2018, 10, 28, 17, 23, 15);

            Console.WriteLine(anyDate.BeginOfDay()
                .ToString("yyyy-MM-dd HH:mm:ss")); // writes '2018-10-28 00:00:00'

            Console.WriteLine(anyDate.EndOfDay()
                .ToString("yyyy-MM-dd HH:mm:ss")); // writes '2018-10-28 23:59:59'

            Console.WriteLine(anyDate.BeginOfWeek()
                .ToString("ddd, yyyy-MM-dd HH:mm:ss")); // writes 'Sun, 2018-10-28 00:00:00'

            Console.WriteLine(anyDate.EndOfWeek()
                .ToString("ddd, yyyy-MM-dd HH:mm:ss")); // writes 'Sat, 2018-11-03 23:59:59'

            Console.WriteLine(anyDate.BeginOfMonth()
                .ToString("yyyy-MM-dd HH:mm:ss")); // writes '2018-10-01 00:00:00'

            Console.WriteLine(anyDate.EndOfMonth()
                .ToString("yyyy-MM-dd HH:mm:ss")); // writes '2018-10-31 23:59:59'

            Console.WriteLine(anyDate.BeginOfYear()
                .ToString("yyyy-MM-dd HH:mm:ss")); // writes '2018-01-01 00:00:00'

            Console.WriteLine(anyDate.EndOfYear()
                .ToString("yyyy-MM-dd HH:mm:ss")); // writes '2018-12-31 23:59:59'
        }
    }
}


May the snippet help you as well, to minimize your code base.
I'm pretty sure I will post some more extensions for the DateTime data type, so stay tuned...

No comments:

Post a Comment