1

var start = "13.04.2019 10:00:00"; var end = "14.04.2019 16:00:00";

I need to loop minute by minute. please help me.

// var start = "13.04.2019 10:00:00";
// var end = "14.04.2019 16:00:00";

var start = new Date(2019,05,13,11,00,00,00);
var end = new Date(2019,05,14,16,00,00,00);

var loop = start;
while(loop <= end){
   console.log(loop);

   var loop = loop.setTime(loop.getMinutes() + 1);
}

not working :(

0

3 Answers 3

2

You should use setMinutes instead of setTime You can modify your code to this and everything will work,

var start = new Date(2019,05,13,11,00,00,00);
var end = new Date(2019,05,14,16,00,00,00);
var loop = start;
while(loop <= end){
    loop.setMinutes(loop.getMinutes() + 1);
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can change to this

loop = loop.setTime(loop.getMinutes() + 1);

it will change type of loop to number and cant call getMinutes()

var start = new Date(2019,05,13,11,00,00,00);
var end = new Date(2019,05,14,16,00,00,00);

var loop = start;
while(loop <= end){
   console.log(loop);

   loop.setTime(loop.getTime() + 1000 * 60);
}

Comments

2

use moment library for date operations

    var start = moment('13-04-2019 10:00','DD-MM-YYYY HH:mm');
    var end = moment('14-04-2019 16:00','DD-MM-YYYY HH:mm');

    var diffInMinutes = end.diff(start,'minutes');
    var res=moment('13-04-2019 10:00','DD-MM-YYYY HH:mm');

    for(let i=0;i<diffInMinutes;i++){
       console.log(res.add(1,'minutes'));
    }

sample output

   13-04-2019 10:01
   13-04-2019 10:02
   13-04-2019 10:03
   13-04-2019 10:04
   13-04-2019 10:05

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.