'Recurring Event between date range in JavaScript

Basically, I need to create an Event, every event will be associated with the fields startDate and endDate.

    "name": "First Event",
    "teamName": "Team A",
    "includeMe": true,
    "members": []
    "start": "2022-05-12T01:50:00.127Z", 
    "end": "2022-05-12T01:50:30.127Z",

now, these dates represent only a period in which a particular event will occur multiple times.

every event will have a third field occurrence, this will decide when that event will occur in that time period

    "name": "First Event",
    "teamName": "Team A",
    "includeMe": true,
    "members": []
    "start": "2022-05-12T01:50:00.127Z", 
    "end": "2022-05-12T01:50:30.127Z",
    "occurrence": {}

The structure of the occurrence object is a bit complicated, I didn't specify any structure or schema of that object but let me explain what it will contain.

"occurrence": {}

//
examples: anyone condition will be associated with the occurrence
1. occur every 10(variable) day
2. occur every 2(variable) weeks on Monday(variable) and Friday(variable) 
3. 
   a. occur every 3(variable) month on 21st(variable) day  //OR
   b. occur every 2(varible) months on the second(variable) Monday(variable)
4. 
   a. occur every 2(variable) Year on May(variable) 31st(variable) //OR
   b  occur every First(variable) Monday(variable) of July((variable)
//

For more clearance, I recommend referring Microsoft Teams meeting scheduling form > SET RECURRENCE FORM


I have tried This Schema for Occurence

      occurrence: {
        repeatEvery: {
          on: {
            type: String,
          },
          type: {
            type: String,
            enum: ['day', 'weak', "month", "year"],
          },
          week: [{type: Number}], //week
          month: { //month
           day: {type: Number}, //If day is Null then FixedDay will have Value
           fixedDay: {
            on: { type: String},
            dayName: { type: String}
           }
          }, 
          year: { //Year
           monthDay: { //If monthDay is Null fixedDay will have value
            day: {type: Number},
            month: {type: String}},
           fixedDay: {
            on: { type: Number},
            dayName: { type: String},
            month: { type: String}
           }
          }
        }
      }

OUTPUT Section

  1. How will be the schema of occurrence

  2. There is a function that will run every day

    function details

    1. get all events
    2. for each event if(currentDate > event.startDate && currentDate < event.endDate) && if(currentDate >= event.occurrence) send notification to all event.members

Question for the second point

How will currentDate and event.occurrence will be comparable?

I Tried to set Another field with Event i.e nextOccurence if event created for first time then occurrence Object will parse to to Date formate to assigned to nextOccurence then if function run again for this event then it will check if(currentDate >= event.nextOccurence) if true then send reminder and again set nextOccurence by parsing occurence object to next value

async function nextOccurrence(occurrence, start) { // (occurrence object, previous occurence date or start date)
    let currentDate = new Date();

    if(occurrence.repeatEvery.type === "day") {
        let every = occurrence.repeatEvery.number //${every} day
        let occurenceDate = start
        occurenceDate.setDate(start.getDate() + noDays)
        return occurenceDate;
    } else if (occurrence.repeatEvery.type === "week") {
        let every = occurrence.repeatEvery.number //${every} week
        let weekDays = occurrence.repeatEvery.week // Array Of week Days {Must be Sorted}
        let dayDates = []
        await weekDays.forEach(day => {
            let occurenceDate = start
            let startDay = occurrence.getDay();
            let distance = day - startDay;
            occurrence.setDate(occurrence.getDate() + distance)
            dayDates.push(occurrence)
            dayDates.sort((a, b) => b.date - a.date)
        });

        dayDates.forEach(occurenceDate => { //Is this Right?
            if(currentDate <= occurenceDate) {
                return occurenceDate
            }
        })
    } else if (occurrence.repeatEvery.type === "month") {
        
        
    } else if (occurrence.repeatEvery.type === "year") {
        
    }

but in this case If week have multiple values then how will I compare it



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source