[Fixed] How to generate the array of time durations

Issue

I’m trying to create an array of time durations in a day with 15 minute intervals with moment.js and ES6.

Example: let time_duration= ["15 Min", "30 Min",..., "1 hr 15 min", "1 hr 30 min"]

Solution

I think it supposed to work:

  generateTimeDurations(minutesGap: number, length: number): string[] {
    const result = Array(length).fill(null);
    let acc = 0;
    return result.map(_ => {
      acc += minutesGap;
      if (acc >= 60) {
        return `${Math.floor(acc / 60)} hr ${acc % 60} min`;
      } else {
        return `${acc} min`;
      }
    });
  }

Leave a Reply

(*) Required, Your email will not be published