I'm assuming you have two Dates as your range delimiters. If not, you can create them this way:
var startDate = new Date('1 March 2020')
var endDate = new Date('29 April 2020')
Then, you have to increase the first date by one day until you reach the last date. To increase the first date by one day you can do this:
startDate.setDate(startDate.getDate() + 1)
You can get the day and the month from a Date with date.getDay()
and date.getMonth()
, but those methods will return numbers. To get the actual names you can do this:
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
var startDateDay = days[startDate.getDay()]
var startDateMonth = months[startDate.getMonth()]
And then, you iterate:
var period = []
while (startDate <= lastDate) {
period.push({
day: days[startDate.getDay()],
date: startDate.getDate(),
month: months[startDate.getMonth()],
year: startDate.getYear()
})
startDate.setDate(startDate.getDate() + 1)
}
And that's it. Here's a fiddle with a working example:
var startDate = new Date('1 March 2020')
var endDate = new Date('29 April 2020')
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
var period = []
while (startDate <= endDate) {
period.push({
day: days[startDate.getDay()],
date: startDate.getDate(),
month: months[startDate.getMonth()],
year: startDate.getFullYear()
})
startDate.setDate(startDate.getDate() + 1)
}
console.log(period)