Thursday, September 16, 2010

Count number of times day will fall in a month of a year

People who starts learning any programing language have the problem that how they can find out no. of days in a particular month of a year. Sometimes it has been given as assignments to them and sometimes our real world programming creates the situation where we have to do the same, recently I was surfing a forum site and stop by on the same question so thought to write an article about it.


I will write the function in two ways first the traditional approach and second will implement Linq in that

First: The traditional implementation:

Create a Function which will return an integer value of times the particular day will occur in a particular month of the year

/// <summary>
        /// Count number of times day will fall in a month of a year
        /// </summary>
        /// <param name="year">Year in which the month will fall</param>
        /// <param name="month">Month for which we need to count the no of days</param>
        /// <param name="dayOfWeek">day of week which we wnt to count</param>
        /// <returns>Number of times the day will fall in that month</returns>
        static int NumberOfParticularDaysInMonth(int year, int month, DayOfWeek dayOfWeek)
        {
            //create a variable to start from date i.e. 1st of that month in thta year which has passed as arrgument
            DateTime startDate = new DateTime(year, month, 1);
            //use the function of DateTime class which will return the no of days in thta particular month
            int days = DateTime.DaysInMonth(startDate.Year, startDate.Month);
            //create and initalize a variable with 0 which will hold no of occurnces in that month
            int weekDayCount = 0;
            //create a loop to count the number of occurncess of that day
            for (int day = 0; day < days; ++day)
            {
                //add days to the start date and check its day of week is what we are counting for if yes add 1 to our variable else add 0
                weekDayCount += startDate.AddDays(day).DayOfWeek == dayOfWeek ? 1 : 0;
            }
            //return the count variable
            return weekDayCount;
        }

Now what we need is just to call above function will be like this


int CountDayOfWeekInMonth = NumberOfParticularDaysInMonth(2010, 9, DayOfWeek.Friday);


The Linq Implementation of the same function will go like this


  /// <summary>
        /// Count number of times day will fall in a month of a year
        /// </summary>
        /// <param name="year">Year in which the month will fall</param>
        /// <param name="month">Month for which we need to count the no of days</param>
        /// <param name="dayOfWeek">day of week which we wnt to count</param>
        /// <returns>Number of times the day will fall in that month</returns>
        static int NumberOfParticularDaysInMonth(int year, int month, DayOfWeek dayOfWeek)
        {
            //create a variable to start from date i.e. 1st of that month in thta year which has passed as arrgument
            DateTime startDate = new DateTime(year, month, 1);
            //use the function of DateTime class which will return the no of days in thta particular month
            int days = startDate.AddMonths(1).Subtract(startDate).Days;
            //use Linq to get the no of occurance of that day
            int weekdaycount = Enumerable.Range(1, days)
                .Select(item => new DateTime(year, month, item))
                .Where(date => date.DayOfWeek == dayOfWeek)
                .Count();
            //return the count variable
            return weekdaycount;
        }


Now you can call this above function using the following statement

int CountDayOfWeekInMonth = NumberOfParticularDaysInMonth(2010, 9, DayOfWeek.Friday);


Regards, May Lord Shiva Bless all of us 
Meetu Choudhary
Microsoft MVP (ASP.Net)

DNS MVM Awardee | Microsoft Certified Professional | Microsoft Certified Technology Specialist |
Co-founder / Webmaster :
www.jaipurmentor.com | www.msdotnetmentor.com | www.indiaalt.net | Lead Editor / Webmaster : www.dotnetspider.com | www.silverlightclub.com  | interview.msdotnetheaven.com | forum.msdotnetheaven.com | My Blog : http://aspnetbymeetu.blogspot.com | My Profile : www.google.com/profiles/meetuchoudhary

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner

MSDotnetMentor

MSDotnetMentor My Website http://msdotnetmentor.com