Reputation: 3551
I want to get the an array of next seven days INCLUDING current one in array using moment.js, I almost got it working but there is a problem, the next day following the first one (the current day) is skipped for some reason.
So if today is friday, saturday will be skipped (not appearing in my final array) and the other days are good.
My method to construct the array:
const getNext7Days = () =>
{
let days = [];
let finalDays = [];
let daysRequired = 7
for (let i = 1; i <= daysRequired; i++) {
if(i==1)
{
days.push( moment().format('dddd, D MMMM YYYY') )
}
else
{
days.push( moment().add(i, 'days').format('dddd, D MMMM YYYY') )
}
}
//Alert.alert(' '+days.length);
for (let x = 0; x < days.length; x++)
{
var dayId = moment(days[x],'dddd, D MMMM YYYY').day();
var dayLetter = moment(days[x],'dddd, D MMMM YYYY').format('dddd');
var dayNumber = moment(days[x],'dddd, D MMMM YYYY').format('Do');
finalDays.push({ dayId:dayId, dayLetter:dayLetter, dayNumber:dayNumber });
}
console.log(finalDays);
setDays(finalDays);
};
Upvotes: 0
Views: 248
Reputation: 148
In your first loop you start i = 1, so the first iteration is current day, the second iteration add 2 days (i = 2). Start at i = 0 should do what you want.
Upvotes: 1