D
denkyira
Guest
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.
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}
EndMonth {5/23/2019 12:00:00 AM}---Correct Date and day-Thursday
[1]
StartDateMonth {5/30/2019 12:00:00 AM}
EndMonth {6/28/2019 12:00:00 AM}---- Incorrect Date and Day Friday
[2]
StartDateMonth {6/27/2019 12:00:00 AM}
EndMonth {7/25/2019 12:00:00 AM}----Correct Date and Day Thursday
[3]
StartDateMonth {7/25/2019 12:00:00 AM}
EndMonth {8/23/2019 12:00:00 AM}-- ---Incorrect Date and day Friday
[4]
StartDateMonth {8/29/2019 12:00:00 AM}
EndMonth {9/27/2019 12:00:00 AM}------ ---- Incorrect Date and Day Friday
Ebenezer
Continue reading...
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);
months.Add(new BusinessWeekDays{ StartDateMonth = _dtNew, EndMonth = e});
Console.WriteLine(months);
}
}
}
Results:
------------
[0]
StartDateMonth {4/25/2019 12:00:00 AM}
EndMonth {5/23/2019 12:00:00 AM}---Correct Date and day-Thursday
[1]
StartDateMonth {5/30/2019 12:00:00 AM}
EndMonth {6/28/2019 12:00:00 AM}---- Incorrect Date and Day Friday
[2]
StartDateMonth {6/27/2019 12:00:00 AM}
EndMonth {7/25/2019 12:00:00 AM}----Correct Date and Day Thursday
[3]
StartDateMonth {7/25/2019 12:00:00 AM}
EndMonth {8/23/2019 12:00:00 AM}-- ---Incorrect Date and day Friday
[4]
StartDateMonth {8/29/2019 12:00:00 AM}
EndMonth {9/27/2019 12:00:00 AM}------ ---- Incorrect Date and Day Friday
Ebenezer
Continue reading...