0

I'd like to count from a date to another one using a while loop and count the day + 1 till it comes to the final date.

So as example:

var start = new Date(2011, 08, 15);
var end = new Date(2017, 09, 28);
var expired = false;

while (!expired) {
    // very first iteration would count day + 1 so it will be 2011-08-16
    // and so on till 2017-09-28
    // check if end date and set expired to true, finished while loop
}

alert("Finished!");

I don't know how to do this (other anwers didn't help). Any ideas?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
webta.st.ic
  • 4,781
  • 6
  • 48
  • 98

3 Answers3

1

You can use .getDate() and .setDate() methods, in start.setDate(start.getDate() + 1) to increment the date by one day, this is how should be your code:

while (!expired) {
    start.setDate(start.getDate() + 1);
    // and so on till 2017-09-28
    if(dateDiffInDays(start, end) == 0) {
       expired = true;
    }
    count++;
}
console.log(count);
alert("Finished!");

Demo:

// A useful function to get dates diff in days gotten from: https://stackoverflow.com/a/15289883/3669624
function dateDiffInDays(a, b) {
  var _MS_PER_DAY = 1000 * 60 * 60 * 24;  
  var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
  var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
  return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}

var start = new Date(2011, 08, 15);
var end = new Date(2017, 09, 28);
var expired = false;

var count =0;

while (!expired) {
    start.setDate(start.getDate() + 1);
    // and so on till 2017-09-28
    if(dateDiffInDays(start, end) == 0) {
       expired = true;
    }
    count++;
}
console.log(count);
alert("Finished!");

The start date will be increased by one day in each iteration until it reachs the same day as the end date, and finally in the end count variable will hold the days between these two days.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0

Use getDate method

var start = new Date(2011, 08, 15);
var end = new Date(2017, 09, 28);
var expired = false;
var count = 0;
while (!expired) {
count++;
  if(start.getYear() == end.getYear() && start.getMonth() == end.getMonth() && start.getDate() == end.getDate()){
    expired = true;
  }
  start.setDate(start.getDate()+1);
}
console.log(count);
alert("Finished!");
Nemani
  • 778
  • 5
  • 12
  • That isn't correct. It only loops 14 times from 15 to 28 Sep, it doesn't get to 28 Oct. – RobG Sep 19 '17 at 08:48
  • it is supposed to loop 14 times between 15 to 28, and it gets 28, did you try this code?, whats your expectation? – Nemani Sep 19 '17 at 08:54
  • @RobG, as you have down voted this answer, at least let me know whats your expectation is? – Nemani Sep 19 '17 at 11:40
  • The OP expects it to loop "*from a date … to the final date*". My expectation is that if the start date is 15 Sep and the final date is 28 Oct, that will loop 43 times from the start to the end. This answer stops at 28 Sep, it never reaches the final date. – RobG Sep 19 '17 at 22:50
  • @RobG you were right, also i had to consider year, so it should loop through (6*365+ 48) times, updated code – Nemani Sep 21 '17 at 04:55
0

You can use getTime() method.

  1. Convert dates into milliseconds using getTime().
  2. Add 86400 seconds (total seconds per day) to start per loop iteration.
  3. Wait until alert (loop is done).

NOTE

new Date(2011, 08, 15) is 15 September 2011, instead of August.

new Date(2017, 09, 28) is 28 October 2017, instead of September.

See this as reference or checkout the code snippet below.

var start = new Date(2011, 08, 15).getTime();  // convert time into milliseconds
var end = new Date(2017, 09, 28).getTime();    // convert time into milliseconds
var expired = false;
var count = 0;

/* NOT 15 AUGUST 2011 */
console.log(new Date(2011, 08, 15).toString());
console.log(new Date(2017, 09, 28).toString());
/* NOT 28 SEPTEMBER 2017 */

while (!expired) {
    start += 24*60*60;  // One day has 24 hours * 60 minutes * 60 seconds = 86400 seconds
    count++;
    if (start >= end) expired = true;
}

alert(`Days In Between: ${count} days`);
yqlim
  • 6,898
  • 3
  • 19
  • 43
  • 1
    You should not add days by adding 24 hours since where daylight saving is observed, days are not 24 hours long on 2 days of the year. Adding days to dates has been covered extensively in answers to other questions. – RobG Sep 19 '17 at 08:50
  • @RobG daylight saving problem has never ever come to my mind because where I live doesn't practice daylight saving. thanks for pointing that out. – yqlim Sep 19 '17 at 09:00
  • Nor is there daylight saving where I am, as those who observe it constantly remind me. ;-) – RobG Sep 19 '17 at 22:52