Home > database >  Find the hours and minutes of a cycle in one day
Find the hours and minutes of a cycle in one day

Time:12-29

For a mini project i need to find the next time of one cycle in one day. we have a rotor that it is working 40 minutes and it is standby also for 40 minutes, if time of his start is 11:40, we should find all the next times of his starts in next 24 h by hours and minutes, like : 13:00, 14:20 etc.

const time = new time('11:40');

time.setMinutes(time.getMinutes()   40);


console.log(time); 

CodePudding user response:

The solution is quite case specific, but it can be easily made more generic. First, you can easily work with dates in VanillaJS using the Date object.

We can deduce the rotor starts every 80 minutes, so the proposed solution adds 80 minutes to the start Date at every iteration.


const minuteInterval = 40 * 2;
const minutesPerDay = 24 * 60;
const intervals = Math.floor(minutesPerDay / minuteInterval);

let moment = new Date('2022-12-28T11:40:00.000Z');
for (let i = 0; i < intervals; i  ) {
    moment = new Date(moment.getTime()   minuteInterval * 60000);
    console.log(moment);
}
  • Related