none
Calculating the number of months and days between two dates RRS feed

  • Question

  • I need the following.

    I want to calculate the number of days in each months along with the month name from a selected range of two dates.

    For example:

    Start Date: 25th August, 2012
    End Date: 4th September, 2012

    Then I need the output as follows:

    7 days in the month of August, 2012
    4 days in the month of September, 2012

    Tuesday, September 4, 2012 5:06 AM

Answers

  • Good enough solution might be just iterate all days in range and keep track of how many days are in each month at each year and this way you only need to iterate dates once. Knowing if month is full or not might give you performance benefit, but getting that information might require you to iterate dates two times.

    For example

    class Program
    {
        enum Months
        {
            January = 1,
            February = 2,
            March = 3,
            April = 4,
            May = 5,
            June = 6,
            July = 7,
            August = 8,
            September = 9,
            October = 10,
            November = 11,
            December = 12
        }
    
        static void Main(string[] args)
        {
            DateTime begin = new DateTime(2012, 8, 25);
            DateTime end = new DateTime(2012, 9, 4);
    
            int month = 0;
            int days = 0; 
            int year = 0;
    
            for (int i = 0; i < end.Subtract(begin).Days + 1; i++)
            {
                DateTime date = begin.AddDays(i);
    
                //// set year if it changes
                if (year != date.Year)
                {
                    year = date.Year;
                }
    
                //// set month if it changes
                if (month != date.Month)
                {
                    //// print result of previous month if valid
                    if (month > 0)
                    {
                        Console.WriteLine("{0} days in {1} {2}", days, (Months)month, year);
                    }
    
                    month = date.Month;
                    days = 0;
                }
    
                //// keep record of days
                days++;
            }
    
            //// last month
            Console.WriteLine("{0} days in {1} {2}", days, (Months)month, year);
    
            Console.ReadKey();
        }
    }


    • Marked as answer by Amey Kelkar Tuesday, September 4, 2012 7:30 AM
    Tuesday, September 4, 2012 7:15 AM

All replies

  • Good enough solution might be just iterate all days in range and keep track of how many days are in each month at each year and this way you only need to iterate dates once. Knowing if month is full or not might give you performance benefit, but getting that information might require you to iterate dates two times.

    For example

    class Program
    {
        enum Months
        {
            January = 1,
            February = 2,
            March = 3,
            April = 4,
            May = 5,
            June = 6,
            July = 7,
            August = 8,
            September = 9,
            October = 10,
            November = 11,
            December = 12
        }
    
        static void Main(string[] args)
        {
            DateTime begin = new DateTime(2012, 8, 25);
            DateTime end = new DateTime(2012, 9, 4);
    
            int month = 0;
            int days = 0; 
            int year = 0;
    
            for (int i = 0; i < end.Subtract(begin).Days + 1; i++)
            {
                DateTime date = begin.AddDays(i);
    
                //// set year if it changes
                if (year != date.Year)
                {
                    year = date.Year;
                }
    
                //// set month if it changes
                if (month != date.Month)
                {
                    //// print result of previous month if valid
                    if (month > 0)
                    {
                        Console.WriteLine("{0} days in {1} {2}", days, (Months)month, year);
                    }
    
                    month = date.Month;
                    days = 0;
                }
    
                //// keep record of days
                days++;
            }
    
            //// last month
            Console.WriteLine("{0} days in {1} {2}", days, (Months)month, year);
    
            Console.ReadKey();
        }
    }


    • Marked as answer by Amey Kelkar Tuesday, September 4, 2012 7:30 AM
    Tuesday, September 4, 2012 7:15 AM
  • Pretty Good so far. Just validating your code and it seems to work very well.
    Thanks brother!

    Any more suggestions are welcomed! 

    • Edited by Amey Kelkar Tuesday, September 4, 2012 7:32 AM
    Tuesday, September 4, 2012 7:31 AM
  • Addition of this code is also possible

    //To swap begin and end dates if end date is greater than begin date 
                if (end < begin)
                {
                    DateTime temp;
                    temp = begin;
                    begin = end;
                    end = temp;
                }


    Tuesday, September 4, 2012 8:01 AM