Your i
variable is the number of the day to display. Your firstDate
variable is a Date
.
This line:
var d = new Date(firstDate + i);
adds these together and tries to create a new Date
. Due to these being different types (a date and a number) type coercion comes into play (ie: the Date and the Number get converted to strings, and concatenated together).
Try this:
var DAY_IN_MS = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
var theDate = new Date("2017-04-10");
var i = 5;
// Date + Number = Type Coercion! Both objects will be converted to strings and concatenated together.
alert(theDate + i);
// The Date constructor doesn't care, and will work because it can get a date from the first part of the string.
alert(new Date(theDate + i));
// If you use `getTime()` you can get the numerical value of the Date, which you can use for arithmetic.
alert(theDate.getTime());
// By adding `i`, however, you are only adding a few milliseconds to the first date !!
alert(new Date(theDate.getTime() + i);
// Number + Number of days times milliseconds per day = success!
alert(new Date(theDate.getTime() + (i * DAY_IN_MS)));
I think you mean:
var d = new Date(firstDate.getTime() + (i * Day));