Reputation: 40381
I want to know how to calculate the last date of this pay period?
I know that the pay is bi-weekly and the first period started on 01/09/2012. so far here what i have done
DateTime d = new DateTime();
d = Convert.ToDateTime("01/09/2012");
while (d <= Convert.ToDateTime("01/06/2013")) {
PayPeriod.Items.Add(new ListItem(d.ToString("MM/dd/yyyy"), d.ToString("MM/dd/yyyy")));
d = d.Date.AddDays(14);
}
And this work perfect, but it work perfect because I have manually put the ending of the current pay period "01/06/2013".
My question is how can I automatically figure out the last date of the current pay period?
Upvotes: 1
Views: 2173
Reputation: 2242
You can easily do this with the following logic:
DateTime startTime = new DateTime(2012,09,01);
DateTime now = DateTime.Now;
var diff = now.Subtract (startTime);
int daysToEndPeriod = diff.Days % 14;
if (daysToEndPeriod == 0)
Console.WriteLine("end of pay period");
else
Console.WriteLine("end of pay period is: " + DateTime.Now.AddDays(14-daysToEndPeriod).Date);
This works because you'll always get the Modulo operator returns how many days past the pay period you have left.
Upvotes: 4