Reputation: 43
Using this code I am getting the dates excluding the start dates
var startdate = new Date("");
var enddate = new Date("");
while (startdate < enddate) {
startdate.setDate(startdate.getDate() + 1);
dates.push(new Date(startdate).format("mm/dd/yyyy"));
}
Upvotes: 2
Views: 353
Reputation: 806
You can't simply format javascript date using format function, which doesn't exists. You need to split date, month, year and join. Here it may help-
var startdate = new Date();
var enddate = new Date("2019, 10, 23");
while (startdate < enddate) {
startdate.setDate(startdate.getDate() + 1);
dates.push(startdate.getMonth()+1+"/"+startdate.getDate()+"/"+startdate.getFullYear());
}
You have to increase moth by 1 startdate.getMonth()+1
because month starts from 0 in JS.
Consider using Moment.js which has lots of facility.
Upvotes: 0
Reputation: 374
You should replace new Date(startdate)
with new Date(startdate.getTime())
to avoid JS error. However, you cannot simply format JS Date object with format()
, as this function does not exist, you have to extract year, month, day by yourself. If you want to do it, beware the month value is started from zero.
Please consider using a library like Moment.js instead, it is a lot handy to manipulate date, time and duration.
Upvotes: 1