Reputation: 512
I am very close to getting this to work and hope someone here can help push the ball over the top of the hill. In C#, I need to get the most recent Payday and the next Payday from today's date (and will eventually expand this to find any pay date in the company's history). Payday occurs every other Friday. I am using a baseline date from history to find my pay dates.
My assumption (please correct if I am wrong) is that I should always be able to divide my pay date by 14. If I cannot, then I must be on a non-pay week.
Here is what I have done thus far:
public DateTime GetNextPayDay()
{
DateTime myNow = DateTime.Now;
string myDay = myNow.DayOfWeek.ToString();
while (myDay != "Friday")
{
myNow = myNow.AddDays(1);
myDay = myNow.DayOfWeek.ToString();
}
DateTime mySeedDate = DateTime.Parse("01/02/1970");
TimeSpan myTimeSpan = myNow.Subtract(mySeedDate);
int myDaysDifference = myTimeSpan.Days;
if (myDaysDifference % 14 == 0) // Problem is here
{
myNow = myNow.AddDays(7);
}
return myNow;
}
public DateTime GetLastPayDay()
{
DateTime myNow = DateTime.Now;
string myDay = myNow.DayOfWeek.ToString();
while (myDay != "Friday")
{
myNow = myNow.AddDays(-1);
myDay = myNow.DayOfWeek.ToString();
}
DateTime mySeedDate = DateTime.Parse("01/02/1970");
TimeSpan myTimeSpan = myNow.Subtract(mySeedDate);
int myDaysDifference = myTimeSpan.Days;
if (myDaysDifference % 14 == 0) // Problem is here
{
myNow = myNow.AddDays(-7);
}
return myNow;
}
Upvotes: 2
Views: 347
Reputation: 12815
Your issue is the
if (myDaysDifference % 14 == 0)
should be a not equal
if (myDaysDifference % 14 != 0)
in both methods.
Your logic is currently finding the previous Friday from now and then seeing if the difference between that and the first date is divisible by 14 and if so you are adding 7 days to it. This is incorrect as if the gap is divisible by 14 then myDay
must be a payday. You only want to add 7 if myDaysDifference
doesn't divide exactly by 14 as this would mean you have a Friday that isn't a pay day.
Upvotes: 5