根据重复日期和重复周期计算事件发生前的天数
Calculate how many days until an event, based on a recurring date and recurrence
我正在尝试构建一个函数来计算距事件发生还有多少天。 “日期”是事件开始的时间,“重复”处理事件的重复,我尝试构建的函数迭代数据并返回一个数组,其中包含新的下一个日期事件以及在此之前还有多少天。
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' },
];
也许任何人都可以帮助我获得更好的性能,但这是我引以为豪的工作。
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)
我正在尝试构建一个函数来计算距事件发生还有多少天。 “日期”是事件开始的时间,“重复”处理事件的重复,我尝试构建的函数迭代数据并返回一个数组,其中包含新的下一个日期事件以及在此之前还有多少天。
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' },
];
也许任何人都可以帮助我获得更好的性能,但这是我引以为豪的工作。
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)