Home > Back-end >  Show next actual time within date loop
Show next actual time within date loop

Time:12-26

I'm using a script which adds 210 minutes to a certain datetime until a specific end date is reached. This is working so far.

Now I'm searching a solution that only the next actual date is shown, and not all until the end date.

Example: if the actual date is the 25.12.2022 18:22 the shown result should be 25.12.2022 19:30

At the moment I don't get any results showing. There seems to be an error in the condition:

JSFiddle

 if (actualtime <= date.toLocaleString('de-DE') && actualtime >= date.toLocaleString('de-DE')) {
function addMinutes(date, minutes) {
    let newDate = new Date(date);
    newDate.setMinutes(newDate.getMinutes()   minutes);
    return newDate;
}

let date = new Date('2022-12-24T16:00:00');
let endDate = new Date('2022-12-31T16:00:00');
let actualtime= new Date().toLocaleString('de-DE');

let deltaMinutes = 210;
while (date <= endDate) {
  if (actualtime <= date.toLocaleString('de-DE') && actualtime >= date.toLocaleString('de-DE')) {
      console.log(date.toLocaleString('de-DE'));
      date = addMinutes(date, 210);
  }
}

CodePudding user response:

Maybe the following does what you want. It will keep adding 210 minutes to the datetime value until the datetime value is larger than the current time:

function addMinutes(date, minutes) {
let newDate = new Date(date);
newDate.setMinutes(newDate.getMinutes()   minutes);
return newDate;
}

let date = new Date('2022-12-24T16:00:00');
let endDate = new Date('2022-12-31T16:00:00');
let now=new Date(), actualtime=now.toLocaleString('de-DE');
let deltaMinutes = 210;
while (date <= now) date = addMinutes(date, 210);
console.log(date.toLocaleString('de-DE'));

  • Related