0
function dateColumn() {
    var startDate = new Date($("#anStartDate").val()); //the start date
    var pmtPeriods = $("#anPaymentPeriods").val(); //number of months in this case
    var dateData = new Array(pmtPeriods);
    var i = 0;
    while (i < pmtPeriods) {
        startDate = startDate.add(i).month();
        dateData[i] = startDate;
        i++
    }
    alert(dateData);
}

Let's say my start date is 2014-01-01 and I put 2 months as pmtPeriods. That fills my array with [2014-02-01, 2014-02-01]. If I take pmtPeriods as 3 with same start date result is [2014-03-01, 2014-03-01, 2014-03-01]. This is wrong.

With pmtPeriods 2 I would like result:

[2014-02-01, 2014-03-01]

instead of

[2014-02-01, 2014-02-01]
2
  • 1
    What is your question? Commented Feb 28, 2014 at 15:21
  • "With pmtPeriods 2 i would like result: [2014-02-01, 2014-03-01]" ? Commented Feb 28, 2014 at 15:28

1 Answer 1

3

You're adding startDate to the dateData array 3 times. That's just adding a reference to the startDate value which is getting updated in your loop. Change:

dateData[i] = startDate;

to

dateData[i] = new Date(startDate);
Sign up to request clarification or add additional context in comments.

1 Comment

Oh...sometimes you feel you are so close but can't solve it. This solved my problem (blaming working all day ;-)). Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.