0

我正在尝试构建一个函数来计算距离事件还有多少天。“日期”是事件开始的时间,“重复”处理事件的重复,我试图构建的函数迭代数据并给我一个包含新的下一个日期事件的数组以及距离此事件还有多少天。


let today = '06/17/2021'

const recurrentDate = { title: 'Vacations', date: '06/18/2021', repeat: 'NONE' }

const countdown = { title: 'Vacations', nextDate: '06/18/2021', daysLeft: 1 }

//See that recurrentDate2 > date

const recurrentDate2 = { title: 'Another event', date: '06/15/2021', repeat: 'MONTHLY' }

const countDown2 = { title: 'Another event', nextDate: '07/15/2021', daysLeft: 28 }

我在玩“momentjs”和“moment-recur”,试图构建计算循环的函数和东西。

let date = moment(new Date('06/15/2021'));

let today = moment(new Date('06/17/202'));

let recurrence = date.recur().every(1).month();

let nextDate = recurrence.next(1);

如果我要接收这样的数据,我需要做什么。基于案例并根据重复计算剩余的所有天数。

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/2021', repeat: 'MONTHLY' },
  { title: 'Conference', date: '12/19/2021', repeat: 'MONTHLY' },
];
4

1 回答 1

0

也许任何人都可以帮助我获得更好的表现,但这是我引以为豪的作品。

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)
于 2021-06-20T17:54:43.433 回答