none
Compare two dates C# RRS feed

  • Question

  • I am trying to output a sequence of dates with Inclusive of the ending date. For some reasons if the Day part of the starting date is greater than the Day part of the End Date, the end date is not included. Below is my code with sample output

    static void Main(string[] args)
    {
    
    var StartDate = DateTime.Parse("06/28/2019");
    var EndDate = DateTime.Parse("09/27/2019");
    
    var Daily = 1;
    for (var i = StartDate; i <= EndDate; i = i.AddMonths(Daily))
    {
    
    int month = i.Month;
    int year = i.Year;
    var FirstDay = new DateTime(year, month, 1);
    
    int difference = (int)DayOfWeek.Monday - (int)FirstDay.DayOfWeek;
    int adddays = difference >= 0 ? 21 : 28;
    int days = difference + adddays;
    var ForthTues = FirstDay.AddDays(days);
    Console.WriteLine(ForthTues);
    }
    }
    
    This code above will give the output
    
    6/24/2019
    
    7/22/2019
    
    8/26/2019
    
    but if I change the dates to below,
    
    var StartDate = DateTime.Parse("06/27/2019");
    var EndDate = DateTime.Parse("09/28/2019");
    
    I get the correct output I am looking for which is 
    
    6/24/2019
    
    7/22/2019
    
    8/26/2019
    
    9/23/2019


    Ebenezer

    Wednesday, June 19, 2019 1:59 PM

Answers

  • Good Day Scott,

    Your solution is giving incorrect output. Anyway I got it working by normalizing the start and the End Date. Just to help anybody who runs into such a problem my complete solution below

     static void Main()
            {
                var StartDate = DateTime.Parse("06/28/2019");
                var EndDate = DateTime.Parse("09/27/2019");
    
                var startMonth = StartDate.AddDays(1 - StartDate.Day);
                var endMonth = EndDate.AddDays(1 - EndDate.Day);
    
                for (var i = startMonth; i <= endMonth; i = i.AddMonths(1))
               
                {
                    var difference = (int)DayOfWeek.Monday - (int)i.DayOfWeek;
                    var addDays = difference >= 0 ? 21 : 28;
                    var days = difference + addDays;
                    var fourthTuesday = i.AddDays(days);
                    if (fourthTuesday <= EndDate)
                        Console.WriteLine(fourthTuesday);
                }
                
    
            }


    Ebenezer

    • Marked as answer by denkyira Thursday, June 20, 2019 1:00 PM
    Thursday, June 20, 2019 1:00 PM

All replies

  • You need to remove the Day part from your comparisons.  Construct a date tempEndDate that is the first of the next month EndDate is in and then use i < tempEndDate in the for loop.  Do a test to verify this works if EndDate = Jan 31, I think it will.  You could do similar for the starting date, only need to do one or the other.


    var nextMonth = EndDate.AddMonths(1);

    var tempEndDate = new DateTime(nextMonth .Year, nextMonth .Month, 1);

    for (var i = StartDate; i < tempEndDate; i = i.AddMonths(Daily))

    Wednesday, June 19, 2019 6:44 PM
  • Good Day Scott,

    Your solution is giving incorrect output. Anyway I got it working by normalizing the start and the End Date. Just to help anybody who runs into such a problem my complete solution below

     static void Main()
            {
                var StartDate = DateTime.Parse("06/28/2019");
                var EndDate = DateTime.Parse("09/27/2019");
    
                var startMonth = StartDate.AddDays(1 - StartDate.Day);
                var endMonth = EndDate.AddDays(1 - EndDate.Day);
    
                for (var i = startMonth; i <= endMonth; i = i.AddMonths(1))
               
                {
                    var difference = (int)DayOfWeek.Monday - (int)i.DayOfWeek;
                    var addDays = difference >= 0 ? 21 : 28;
                    var days = difference + addDays;
                    var fourthTuesday = i.AddDays(days);
                    if (fourthTuesday <= EndDate)
                        Console.WriteLine(fourthTuesday);
                }
                
    
            }


    Ebenezer

    • Marked as answer by denkyira Thursday, June 20, 2019 1:00 PM
    Thursday, June 20, 2019 1:00 PM