Answered by:
Find the last thursday of a month between two dates C#

Question
-
User457850011 posted
<div id="threadActionMenu" class="menu thread"> <div value="unsubscribe"> I am trying to display the start and end date for the last Thursday of every month between two days. I am able to get the last Thursday for each month successfully but not able to display the start and the end date. Below is my code.</div> </div> <div class="messageContent"> <div class="container"> <div class="body">
Any help will be appreciated.
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);----> This line is incorrect and I need help here. months.Add(new BusinessWeekDays{ StartDateMonth = _dtNew, EndMonth = e}); Console.WriteLine(months); } } }
The purpose of these two line:
var e = _dtNew.AddMonths(+1).AddDays(-2);
months.Add(new BusinessWeekDays{ StartDateMonth = _dtNew, EndMonth = e});are just to display the start and the end date for each month and that is where I am having an issue. So for example I know the start date is 04/25/2019
the following month Last Thursday is 5/30/2019
the next month last Thursday is 6/27/2019
I just want to display
StartDate EnDate
4/25/2019 5/30/2019
startdate 6/27/2019
Sunday, April 7, 2019 6:36 PM
Answers
-
User36583972 posted
Hi
Thanks for your response. The idea is I am supposed to get the last Thursday of every month from Start Date to End Date from 04/25/2019 all the way to 9/30/2019
This line of my code var e = _dtNew.AddMonths(+1).AddDays(-2); is incorrect because it is failing when there is 31 days in month while it works for the moths where we have 30 days.As in case of 30 days it will add 27 days ( 4 weeks) while in case of 31 days it will add 29 days (4 weeks + 1)
StartDateMonth EndMonth 25/04/2019 12:00:00 AM 23/05/2019 12:00:00 AM --Incorrect is not the last Thursday. The last Thursday of May is 30/05/2019 30/05/2019 12:00:00 AM 28/06/2019 12:00:00 AM---Incorrect because last Thursday of June is 27/06/2019 27/06/2019 12:00:00 AM 25/07/2019 12:00:00 AM--correct 25/07/2019 12:00:00 AM 23/08/2019 12:00:00 AM----Incorrect because the last Thursday of August is 29/08/2019 29/08/2019 12:00:00 AM 27/09/2019 12:00:00 AM-----Incorrect because the last Thursday of September is 26/09/2019 26/09/2019 12:00:00 AM 24/10/2019 12:00:00 AM-----Incorrect because the last Thursday of October is 31/10/2019
Hi denkyira,
You can refer the following sample:
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); var e = returnEndMonth(_dtNew); months.Add(new BusinessWeekDays { StartDateMonth = _dtNew, EndMonth = e }); } foreach (var item in months) { Console.WriteLine("StartDateMonth:" + item.StartDateMonth + "; EndMonth:" + item.EndMonth); } Console.ReadLine(); } public static DateTime returnEndMonth(DateTime sdt) { int alldays = DateTime.DaysInMonth(sdt.Year, sdt.AddMonths(1).Month); DateTime endtime = new DateTime(sdt.Year, sdt.AddMonths(1).Month, alldays); for(int i=0 ; i<= alldays; i++ ) { if(endtime.AddDays(-i).DayOfWeek.ToString()== "Thursday") { endtime = endtime.AddDays(-i); break; } } return endtime; }
Best Regards
Yong Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 8, 2019 7:10 AM
All replies
-
User303363814 posted
I have run your code and it shows six lists of pairs of dates. Can you explain what is wrong with the result that your code generates?
5List<BusinessWeekDays> (1 item)44 StartDateMonth EndMonth 25/04/2019 12:00:00 AM 23/05/2019 12:00:00 AM 5List<BusinessWeekDays> (2 items)44 StartDateMonth EndMonth 25/04/2019 12:00:00 AM 23/05/2019 12:00:00 AM 30/05/2019 12:00:00 AM 28/06/2019 12:00:00 AM 5List<BusinessWeekDays> (3 items)44 StartDateMonth EndMonth 25/04/2019 12:00:00 AM 23/05/2019 12:00:00 AM 30/05/2019 12:00:00 AM 28/06/2019 12:00:00 AM 27/06/2019 12:00:00 AM 25/07/2019 12:00:00 AM 5List<BusinessWeekDays> (4 items)44 StartDateMonth EndMonth 25/04/2019 12:00:00 AM 23/05/2019 12:00:00 AM 30/05/2019 12:00:00 AM 28/06/2019 12:00:00 AM 27/06/2019 12:00:00 AM 25/07/2019 12:00:00 AM 25/07/2019 12:00:00 AM 23/08/2019 12:00:00 AM 5List<BusinessWeekDays> (5 items)44 StartDateMonth EndMonth 25/04/2019 12:00:00 AM 23/05/2019 12:00:00 AM 30/05/2019 12:00:00 AM 28/06/2019 12:00:00 AM 27/06/2019 12:00:00 AM 25/07/2019 12:00:00 AM 25/07/2019 12:00:00 AM 23/08/2019 12:00:00 AM 29/08/2019 12:00:00 AM 27/09/2019 12:00:00 AM 5List<BusinessWeekDays> (6 items)44 StartDateMonth EndMonth 25/04/2019 12:00:00 AM 23/05/2019 12:00:00 AM 30/05/2019 12:00:00 AM 28/06/2019 12:00:00 AM 27/06/2019 12:00:00 AM 25/07/2019 12:00:00 AM 25/07/2019 12:00:00 AM 23/08/2019 12:00:00 AM 29/08/2019 12:00:00 AM 27/09/2019 12:00:00 AM 26/09/2019 12:00:00 AM 24/10/2019 12:00:00 AM Sunday, April 7, 2019 9:25 PM -
User457850011 posted
Hi
Thanks for your response. The idea is I am supposed to get the last Thursday of every month from Start Date to End Date from 04/25/2019 all the way to 9/30/2019
This line of my code var e = _dtNew.AddMonths(+1).AddDays(-2); is incorrect because it is failing when there is 31 days in month while it works for the moths where we have 30 days.As in case of 30 days it will add 27 days ( 4 weeks) while in case of 31 days it will add 29 days (4 weeks + 1)
StartDateMonth EndMonth 25/04/2019 12:00:00 AM 23/05/2019 12:00:00 AM --Incorrect is not the last Thursday. The last Thursday of May is 30/05/2019 30/05/2019 12:00:00 AM 28/06/2019 12:00:00 AM---Incorrect because last Thursday of June is 27/06/2019 27/06/2019 12:00:00 AM 25/07/2019 12:00:00 AM--correct 25/07/2019 12:00:00 AM 23/08/2019 12:00:00 AM----Incorrect because the last Thursday of August is 29/08/2019 29/08/2019 12:00:00 AM 27/09/2019 12:00:00 AM-----Incorrect because the last Thursday of September is 26/09/2019 26/09/2019 12:00:00 AM 24/10/2019 12:00:00 AM-----Incorrect because the last Thursday of October is 31/10/2019
Sunday, April 7, 2019 10:49 PM -
User36583972 posted
Hi
Thanks for your response. The idea is I am supposed to get the last Thursday of every month from Start Date to End Date from 04/25/2019 all the way to 9/30/2019
This line of my code var e = _dtNew.AddMonths(+1).AddDays(-2); is incorrect because it is failing when there is 31 days in month while it works for the moths where we have 30 days.As in case of 30 days it will add 27 days ( 4 weeks) while in case of 31 days it will add 29 days (4 weeks + 1)
StartDateMonth EndMonth 25/04/2019 12:00:00 AM 23/05/2019 12:00:00 AM --Incorrect is not the last Thursday. The last Thursday of May is 30/05/2019 30/05/2019 12:00:00 AM 28/06/2019 12:00:00 AM---Incorrect because last Thursday of June is 27/06/2019 27/06/2019 12:00:00 AM 25/07/2019 12:00:00 AM--correct 25/07/2019 12:00:00 AM 23/08/2019 12:00:00 AM----Incorrect because the last Thursday of August is 29/08/2019 29/08/2019 12:00:00 AM 27/09/2019 12:00:00 AM-----Incorrect because the last Thursday of September is 26/09/2019 26/09/2019 12:00:00 AM 24/10/2019 12:00:00 AM-----Incorrect because the last Thursday of October is 31/10/2019
Hi denkyira,
You can refer the following sample:
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); var e = returnEndMonth(_dtNew); months.Add(new BusinessWeekDays { StartDateMonth = _dtNew, EndMonth = e }); } foreach (var item in months) { Console.WriteLine("StartDateMonth:" + item.StartDateMonth + "; EndMonth:" + item.EndMonth); } Console.ReadLine(); } public static DateTime returnEndMonth(DateTime sdt) { int alldays = DateTime.DaysInMonth(sdt.Year, sdt.AddMonths(1).Month); DateTime endtime = new DateTime(sdt.Year, sdt.AddMonths(1).Month, alldays); for(int i=0 ; i<= alldays; i++ ) { if(endtime.AddDays(-i).DayOfWeek.ToString()== "Thursday") { endtime = endtime.AddDays(-i); break; } } return endtime; }
Best Regards
Yong Lu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 8, 2019 7:10 AM -
User457850011 posted
Hi Lu
Thanks a millions times. Your code works perfectly correct.
Monday, April 8, 2019 9:33 AM