Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Find the Count of a Weekday Between Two Dates with LINQ

5.00/5 (1 vote)
7 Apr 2012CPOL 23.1K  
This is an alternative to using LINQ for Find the count of a weekday between two dates without iterating/looping.

Introduction

Without going into a deep discussion of whether code or compiler is looping or not, it should be noted that LINQ can be very useful for tasks like this.

Further, if you add the methods using extension of DateTime, much flexibility is gained. This alternative demonstrates first how a range of weekdays can be created - offering you a collection of the actual dates - then, how to count the elements of the range which is the original goal of the tip.

Both methods are implemented with very little code which is easy to comprehend, maintain, and expand.

Using the Code

The code separates creation of the range of weekdays and the counting of these.

Using extensions allows for very tight code:

C#
DateTime currentDate = DateTime.Today;
DateTime anotherDate = currentDate.AddDays(23);
DayOfWeek thursday = DayOfWeek.Thursday;
Console.WriteLine(currentDate.WeekdayCount(anotherDate, thursday));
Console.ReadKey(); 

Code

C#
using System;
using System.Collections.Generic;
using System.Linq;                

namespace DateTimeExtension
{
    /// <summary>
    /// Extended methods for System.DateTime.
    /// </summary>
    public static class DateTimeExtensionMethods
    {
        /// <summary>        
        /// Returns the range of dates of the specified weekday
        /// between the current System.DateTime object and another date.
        /// </summary>
        /// <param name="currentDate">The current date</param>
        /// <param name="anotherDate">The other date of the interval to search</param>
        /// <param name="dayOfWeek">The weekday to find</param>
        /// <returns>The collection of dates of the specified weekday within the period.
        /// </returns>
        public static IEnumerable<DateTime> Weekdays(this DateTime currentDate, 
                      DateTime anotherDate, DayOfWeek dayOfWeek)
        {
            int sign = anotherDate.CompareTo(currentDate);
            return Enumerable.Range(0, sign * anotherDate.Subtract(currentDate).Days)
                .Select(delta => currentDate.AddDays(sign * (1 + delta)))
                .Where(date => date.DayOfWeek == dayOfWeek);
        }

        /// <summary>
        /// Returns the count of the specified weekday between
        /// the current System.DateTime object and another date.
        /// </summary>
        /// <param name="currentDate">The current date</param>
        /// <param name="anotherDate">The other date of the interval to search</param>
        /// <param name="dayOfWeek">The weekday to find</param>
        /// <returns>The count of the specified weekday within the period.</returns>
        public static int WeekdayCount(this DateTime currentDate, 
                          DateTime anotherDate, DayOfWeek dayOfWeek)
        {
            return currentDate.Weekdays(anotherDate, dayOfWeek).Count();
        }
    }
}

Usage is included as an in-line brief description.

History

  • 6th April, 2012: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)