Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  73] [ 5]  / answers: 1 / hits: 24549  / 11 Years ago, tue, september 3, 2013, 12:00:00

I looked at some other questions, and don't see my specific problem, so please excuse me if it has been asked or answered.



What I am trying to do is figure out a simple payment calculator, and provide some additional information, such as the first payment date, and the last.



In some cases, the day of the last payment works, and sometimes it doesn't.



Here's my code:



var myDate = new Date();

var odo = document.contract.firstPaymentDate.value;
var n = odo.split(/);
var month = n[0];
var day = n[1];
var year = n[2];

var oldDateObj = new Date(year, month, day);
var newDateObj = new Date(oldDateObj.getTime() + ((document.contract.totalNumberRegularPayments.value - 1)*1209600*1000));
var dd = newDateObj.getDate();
var mm = newDateObj.getMonth();
var y = newDateObj.getFullYear();

var someFormattedDate = mm + '/'+ dd + '/'+ y;
document.contract.lastPaymentDate.value = someFormattedDate;


So I take the first payment date, and add 1209600 seconds times the number of payments (minus 1 since they have already paid the first).



This is based on starting at a specific day that can be chosen by the user.



So my example is 156 BiWeekly payments (so 155 for the calculations), which works out to 6 years. If I choose the date of the 1st, I get 10/01/2013 as the start, but 9/11/2019 as the end (First on a Tuesday, last on a Wednesday).



For the 15th (9/15/2013 - a Sunday - to 8/24/2019 - a Saturday)
For the 20th (9/20/2013 - a Friday - to 8/29/2013 - a Thursday)



So since sometimes it's a day later, and sometimes a day ahead, I can't just +1 to var dd = newDateObj.getDate();



I'm really baffled as to what's going on, and I'm hoping someone out there either has some experience with this, or someone that knows what the heck I might be doing wrong.



Thanks in advance for any help you can offer.


More From » date

 Answers
2

If you want to add a number of weeks, just add 7 times as many days, e.g.



var now = new Date();
// Add two weeks
now.setDate(now.getDate() + 14);


So if you have 24 fortnightly payments:



var now = new Date();
// Add 24 fortnights (48 weeks)
now.setDate(now.getDate() + 24 * 14);


or



now.setDate(now.getDate() + 24 * 2 * 7);


whatever you think is clearest.



If you want to have a start and end date:



var start = new Date();
var end = new Date(+start);
end.setDate(end.getDate() + 24 * 14);

alert('Start on: ' + start + '.nEnd in 24 fortnights: ' + end);


Edit



Here's a working example:



<script>

function calcLastPayment(start, numPayments) {
if (typeof start == 'string') {
start = stringToDate(start);
}

var end = new Date(+start);
end.setDate(end.getDate() + --numPayments * 14)
return end;
}

// Expect date in US format m/d/y
function stringToDate(s) {
s = s.split(/D/)
return new Date(s[2], --s[0], s[1])
}

</script>

<form>
<table>
<tr><td>Enter first payment date (m/d/y):
<td><input name=start>
<tr><td>Enter number of payments:
<td><input name=numPayments>
<tr><td colspan=2><input type=button value=Calc end date onclick=
this.form.end.value = calcLastPayment(this.form.start.value, this.form.numPayments.value)
>
<tr><td>Last payment date:
<td><input readonly name=end>
<tr><td colspan=2><input type=reset>
</table>
</form>


Given a first payment date of Thursday, 5 September and 3 repayments it returns Thursday 3 October, which seems correct to me (5 and 19 September and 3 October). It should work for any number of payments.


[#75945] Sunday, September 1, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rylee

Total Points: 658
Total Questions: 114
Total Answers: 116

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
;