0

how can I get each date between the date1 and date2

var date1 = new Date("06/30/2019");
var date2 = new Date("07/30/2019");

var Difference_In_Time = date2.getTime() - date1.getTime();

var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);

//To display the final no. of days (result) 
document.write("Total number of days between dates  <br>" +
  date1 + "<br> and <br>" +
  date2 + " is: <br> " +
  Difference_In_Days);
Phil
  • 157,677
  • 23
  • 242
  • 245
Choy
  • 452
  • 1
  • 5
  • 19
  • 1
    I'd start by using a better `Date` constructor, eg `new Date(2019, 5, 30)`. See the big warning here ~ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Timestamp_string – Phil Jul 22 '20 at 03:02
  • 1
    Your code appears to be _working_ (though it may struggle with daylight-savings change over times) so what's the problem? – Phil Jul 22 '20 at 03:04
  • Do you want to collect dates inclusive of `date1` and `date2`? – Phil Jul 22 '20 at 03:10
  • 1
    [*Javascript - get array of dates between 2 dates*](https://stackoverflow.com/questions/4413590/javascript-get-array-of-dates-between-2-dates) seems to answer the question. What part of this is specific to vue.js? – RobG Jul 22 '20 at 03:13

1 Answers1

0

If I got your question correctly, then you need to use a for-loop and if you wanna display it in vue, then you'd use v-for directive here is an example of how the code looks like

<template>
  <div id="app">
    <p v-for="day in days" :key="days.indexOf(day)">{{day}}</p>
  </div>
</template>

<script>
  export default {
    name: "App",
    components: {},
    data() {
      return {
        days: []
      };
    },
    created() {
      var date1 = new Date("06/30/2019");
      var date2 = new Date("07/30/2019");
      var Difference_In_Time = date2.getTime() - date1.getTime();
      var Difference_In_Days = Difference_In_Time / (1000 * 3600 * 24);
      for (var i = 0; i < Difference_In_Days; i++) {
        var d = new Date();
        d.setDate(date1.getDate() + i + 1);
        this.days.push(d);
      }
    }
  };
</script>
Yas Dle
  • 1
  • 1