Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to get the last Thursday of every month between two days. Some of my results are correct, others are occurring a day late on Fridays. Below is my code and the results.
Any help will be appreciated.

What I have tried:

C#
public class BusinessWeekDays
        {
            public DateTime StartDateMonth;
            public DateTime EndMonth;
        }      
     
        static void Main(string[] args)
        {
            var StartDate = DateTime.Parse("04/25/2019");
            var SeriesEndDate = DateTime.Parse("09/30/2019");

            var months = new List<businessweekdays>();
            var Monthly = 1;
            for (var i = StartDate; i < SeriesEndDate; i = i.AddMonths(Monthly))
            {
                const int thursday = 4;
                const int dayDiff = 7 - thursday;
                
                var Day = DateTime.DaysInMonth(i.Year, i.Month);
                var DayOfWeek = (int)new DateTime(i.Year, i.Month, Day).DayOfWeek;
                
                var _dtNew = new DateTime(i.Year, i.Month, Day).AddDays(-(DayOfWeek < thursday ? DayOfWeek + dayDiff : DayOfWeek - thursday));
                var e = _dtNew.AddMonths(+1).AddDays(-2);
                months.Add(new BusinessWeekDays{ StartDateMonth = _dtNew, EndMonth = e});
                Console.WriteLine(months);
            }
           }
        }




Results:
------------
[0]	 		
		StartDateMonth	{4/25/2019 12:00:00 AM}	System.DateTime  ----->Correct Date and Day
		EndMonth	{5/23/2019 12:00:00 AM}	System.DateTime
		[1]	 
		StartDateMonth	{5/30/2019 12:00:00 AM}	System.DateTime -----Incorrect Date and Day Friday 
		EndMonth	{6/28/2019 12:00:00 AM}	System.DateTime
		
		[2]	
        StartDateMonth	{6/27/2019 12:00:00 AM}	System.DateTime		
		EndMonth	{7/25/2019 12:00:00 AM}	System.DateTime -----> Correct Date and day 
		
		[3]	 
		StartDateMonth	{7/25/2019 12:00:00 AM}	System.DateTime
		EndMonth	{8/23/2019 12:00:00 AM}	System.DateTime ---Incorrect Date and day
		
		[4]	 
		StartDateMonth	{8/29/2019 12:00:00 AM}	System.DateTime
		EndMonth	{9/27/2019 12:00:00 AM}	System.DateTime ---- Incorrect Date and Day
Posted
Updated 8-Apr-19 10:07am
v2

A linq (lambda) query which "finds" last thursdays of months between two dates:

C#
int datediff = (SeriesEndDate - StartDate).Days+1;
var lastThursdaysBetweenDates = Enumerable.Range(0, datediff)
    .Select(x=>StartDate.AddDays(x))
    .Where(d=> d.DayOfWeek==DayOfWeek.Thursday)
    .GroupBy(d=>d.Month)
    .Select(grp=>grp.Max(d=>d));


Good luck!
 
Share this answer
 
Have a look here: DateTime Extensions to Make Some Simple Tasks a Little More Readable[^] - it may not solve your problem exactly, but it'll give you a base to work from - it includes a "last ...day of the month" method which you could modify quite easily.
 
Share this answer
 
Quote:
Some of my results are correct, others are occurring a day late on Fridays.

May be your evaluation of correctness of result is wrong too.
Quote:
StartDateMonth {4/25/2019 12:00:00 AM} System.DateTime ----->Correct Date and Day
EndMonth {5/23/2019 12:00:00 AM} System.DateTime

Last thursday of May is 30, not 23 !
Check this part
C#
var e = _dtNew.AddMonths(+1).AddDays(-2);

Months have different number of days!

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Member 1283347 7-Apr-19 14:03pm    
I know what my issue is not that I am not debugging my code. Maybe I am not explaining my issue well. The code can give me the last Thursday of each month between two dates correctly.
What I want to do next is just display the start date and the end date for each month between the two days. That is where I am having an issue.
This portion of my code: var e = _dtNew.AddMonths(+1).AddDays(-2);
months.Add(new BusinessWeekDays{ StartDateMonth = _dtNew, EndMonth = e});
is just to show the beginning and end date for each month. I know I am not getting it correct here hence the reason for asking for help. Without these two lines the code gives me correct Last Thursday of every month.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900