0

Just want a help for adding numbers bi-weekly.

Let's say,

Start date : Jan 15, 2012  
End date : May 15, 2012  
Value : 300.00

What I want to accomplish is that, every 15th and last day of the month 300 will be multiplied to how many 15th and last day before May 15, 2012

so

Jan 15, 2012 to Jan 31,    2012 the value must be 300.00  
Feb 01, 2012 to Feb 15,    2012 the value must be 600.00  
Feb 16, 2012 to Feb 28/29, 2012 the value must be 900.00  
Mar 01, 2012 to Mar 15,    2012 the value must be 1200.00  
Mar 16, 2012 to Mar 31,    2012 the value must be 1500.00  
Apr 01, 2012 to Apr 15,    2012 the value must be 1800.00  
Apr 16, 2012 to Apr 30,    2012 the value must be 2100.00  
May 01, 2012 to May 15,    2012 the value must be 2400.00  

hope you get what I mean.

Hoping for your helpful replies, Thanks.

Jhay
  • 77
  • 3
  • 14

1 Answers1

0

You can loop as long as you have whole months, then check if there is a half month to add at the end:

var date = startDate;
var sum = 0;
while (date < endDate) {
  sum += value * 2;
  date.setMonth(date.getMonth() + 1);
}
if (date < endDate) sum += value;
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks for the demo but how about, if my start date is Apr 30, 2012 and the current date is May 1, 2012 the value must still be 0.00 but when my current date became May 15, 2012 that's the only time the value will become 300. Then at the 30th day of May, the value will become 600. Is that possible? – Jhay Apr 27 '12 at 07:32
  • @Jhay: Then it gets a little tricker. I think that the easiest would be to increase the start date day by day until it's either 15 or 1, and reduce the end date the same way. – Guffa Apr 27 '12 at 08:37
  • This really drove me nuts the whole day hehe.. Uki will try till I bang my head on my keyboard :D... thanks though – Jhay Apr 27 '12 at 08:50
  • to only add value every 15th and 30th day of the month from starting date to current date – Jhay Apr 30 '12 at 00:19
  • if I set startDate to 2012-3-30 and endDate to 2012-4-15 the result is still 600 instead of 300 only and if I set startDate to 2012-3-15 and endDate to 2012-3-30 the result is 0 instead of 300 – Jhay Apr 30 '12 at 07:42
  • Add a check so that you don't enter the loop if there isn't a whole month: http://jsfiddle.net/Guffa/WFxhg/3/ – Guffa Apr 30 '12 at 08:28
  • endDate.getDate() - 1 part means the counting starts from the last date of the month to first date? – Jhay Apr 30 '12 at 08:54
  • @Jhay: It finds the previous relevant date, i.e. the first or the 15th. – Guffa Apr 30 '12 at 09:49
  • In your last fiddle, I modified it so that it reads the last date instead of first date of the month. I set startDate to 2012-4-15 and endDate to 2012-5-30 and the result is 1200 instead of 900, see http://jsfiddle.net/EYjqF/ – Jhay May 02 '12 at 01:49
  • can you still help me on this? – Jhay May 07 '12 at 00:12