简体   繁体   中英

Javascript: Calculate array of dates from date input

for a personal project i'm scratching my head around this:

I want to take a date input in the future and a number and return an array of dates (in any format) splitted evenly between the present date and the future date.

Ex: Today is 6 nov, i put 1 Dec and n.5: the function(date, num) takes the date differential in days (this part i got) and returns an array of nuum dates (day format let's assume), each at a date-differential/num time. (Vanilla/jquery/lodash/moment, etc... all allowed)

Some brainstorming or pseudo-code more than welcome!

EDIT: i didn't want to share at first because the app is currently broken and it requires moment to run, anyway:

calculateReminders = (expirationDate, remindersNum) => {
let rightNow = moment();
let expirationDate = moment(expirationDate);
  if (expirationDate !== null && remindersNum > 0) {
  let differential = expirationDate.diff(rightNow, "days");
    if (differential) {
      let reminderFrequency = differential / remindersNum;
        if (reminderFrequency < 1) {
          Alert.alert(
            "some alert"
          );
          return undefined;
        }
      }
    }
  }
}

I am getting the day differential, but i don't create the subsequent dates array i need.

This probably gives you what you need. Hope it helps.

 const daysDifferential = 20; //This part you got const n = 5; // Number of dates to return const increaseOfDays = Math.floor(daysDifferential/n); const arr = []; for(let $i = 0; $i < n; $i++) { const d = new Date(); d.setDate(d.getDate() + $i * increaseOfDays) arr.push(d); } console.log(arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM