I am moving from moment() to dayjs(). I have a loop that creates an array of objects. This loop works in moment but not in dayjs. I am guessing this is because dayjs is Immutable.
Works fine :
let getMonths = () => {
let months = []
let s = moment().startOf('year')
let e = moment().endOf('year')
for (var m = moment(s); m.isBefore(dayjs(e)); m.add(1, 'month')) {
months.push({ id: m.format('MM'), value: m.format('MMM') });
}
return months;
}
Fails :
let getMonths = () => {
let months = []
let s = dayjs().startOf('year')
let e = dayjs().endOf('year')
for (var m = dayjs(s); m.isBefore(dayjs(e)); m.add(1, 'month')) {
months.push({ id: m.format('MM'), value: m.format('MMM') });
}
return months;
}
I need to refactor for dayjs Thanks
I think you should use dayjs(m).isBefore(e)
or m.isBefore(e.format())
to compare two dayjs objects.
let getMonths = () => {
let months = []
let s = dayjs().startOf('year')
let e = dayjs().endOf('year')
for (var m = dayjs(s); m.isBefore(e); m = m.add(1, 'month')) {
months.push({ id: m.format('MM'), value: m.format('MMM') });
}
return months;
}