也许任何人都可以帮助我获得更好的表现,但这是我引以为豪的作品。
var moment = require('moment');
require('moment-recur');
const data = [
{ title: 'Vacations', date: '09/25/2022', repeat: 'NONE' },
{ title: 'Family Event', date: '01/02/2021', repeat: 'DAILY' },
{ title: 'Anniversary', date: '04/19/2021', repeat: 'WEEKLY' },
{ title: 'Friends day', date: '02/27/2021', repeat: 'YEARLY' },
{ title: 'Family plan', date: '10/10/2020', repeat: 'MONTHLY' },
{ title: 'Conference', date: '12/19/2021', repeat: 'MONTHLY' },
];
const newData = data.map(item => {
const dataDate = moment(new Date(item.date))
if ( item.repeat === "NONE" ) {
return {
...item,
nextDate: dataDate < today ? 0 : item.date,
daysLeft: dataDate.diff(today, "days")
}
}
if ( item.repeat === "DAILY" ) {
if (dataDate > today) {
return {
...item,
nextDate: item.date,
daysLeft: dataDate.diff(today, "days")
}
}
if ( dataDate < today ) {
let recurrence = today.recur().every(1).day()
let nextDate = recurrence.next(1)
return {
...item,
nextDate: nextDate[0].format('MM/DD/YYYY'),
daysLeft: nextDate[0].endOf('day').diff(today, "days")
}
} else {
return {
...item,
nextDate: dataDate,
daysLeft: dataDate.endOf('day').diff(today, "days")
}
}
}
if ( item.repeat === "WEEKLY" ) {
if (dataDate > today) {
return {
...item,
nextDate: item.date,
daysLeft: dataDate.diff(today, "days")
}
}
if ( dataDate < today ) {
let getWeeksNumber = today.week() - dataDate.week()
let currentDataDateWeek = dataDate.add(getWeeksNumber, "weeks")
return {
...item,
nextDate: currentDataDateWeek.format('MM/DD/YYYY'),
daysLeft: currentDataDateWeek.endOf('day').diff(today, "days")
}
} else {
return {
...item,
nextDate: dataDate,
daysLeft: dataDate.endOf('day').diff(today, "days")
}
}
}
if ( item.repeat === "MONTHLY" ) {
if (dataDate > today) {
return {
...item,
nextDate: item.date,
daysLeft: dataDate.diff(today, "days")
}
}
if ( dataDate < today ) {
let getMonthsNumber = today.diff(dataDate, "months")
let currentDataDateMonth = dataDate.add(getMonthsNumber, "months")
if (currentDataDateMonth < today) {
let recurrence = currentDataDateMonth.recur().every(1).month()
let nextDate = recurrence.next(1)
return {
...item,
nextDate: nextDate[0].format('MM/DD/YYYY'),
daysLeft: nextDate[0].endOf('day').diff(today, "days")
}
} else {
return {
...item,
nextDate: currentDataDateMonth.format('MM/DD/YYYY'),
daysLeft: currentDataDateMonth.endOf('day').diff(today, "days")
}
}
} else {
return {
...item,
nextDate: dataDate,
daysLeft: dataDate.endOf('day').diff(today, "days")
}
}
}
if ( item.repeat === "YEARLY" ) {
if (dataDate > today) {
return {
...item,
nextDate: item.date,
daysLeft: dataDate.diff(today, "days")
}
}
if ( dataDate < today ) {
let getYearNumber = today.diff(dataDate, "years")
let currentDataDateYear = dataDate.add(getYearNumber, "years")
if (currentDataDateYear < today) {
let recurrence = currentDataDateYear.recur().every(1).year()
let nextDate = recurrence.next(1)
return {
...item,
nextDate: nextDate[0].format('MM/DD/YYYY'),
daysLeft: nextDate[0].endOf('day').diff(today, "days")
}
} else {
return {
...item,
nextDate: currentDataDateYear.format('MM/DD/YYYY'),
daysLeft: currentDataDateYear.endOf('day').diff(today, "days")
}
}
} else {
return {
...item,
nextDate: dataDate,
daysLeft: dataDate.endOf('day').diff(today, "days")
}
}
}
return item
})
console.log(newData)