2012-04-27 44 views
0

只需要雙週添加數字的幫助。在javascript中每兩週添加一次數字

比方說,

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

我想做到的是,一個月300的每15個和最後一天5月15日之前乘以多少月15日和最後一天,2012

所以

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 

希望你明白我的意思。

希望得到您有幫助的回覆,謝謝。

回答

0

您可以循環,只要你有整整兩個月,然後檢查是否有半個月在末尾添加:

var date = startDate; 
var sum = 0; 
while (date < endDate) { 
    sum += value * 2; 
    date.setMonth(date.getMonth() + 1); 
} 
if (date < endDate) sum += value; 
+0

對不起,我還是不能得到這個工作 – Jhay 2012-04-27 06:51:49

+0

@Jhay:演示:http://jsfiddle.net/Guffa/WFxhg/ – Guffa 2012-04-27 06:58:23

+0

感謝您的演示,但如何,如果我的開始日期是2012年4月30日,並且當前日期是2012年5月1日,值必須仍爲0.00,但是當我目前日期成爲2012年5月15日,這是價值將變爲300的唯一時間。然後在5月30日,價值將變爲600.這可能嗎? – Jhay 2012-04-27 07:32:39