How to show dates between a range of dates given with JAVASCRIPT?

1

good afternoon.

My question is the following as I can do to show the dates between 2 dates given by the user only with JavaScript or Jquery, since the information I am getting from an API I only take care of the Frontend and I do not have access to the database.

That is, show dates between 2017-11-29 (November 29, 2017) and 2017-12-05 (December 5, 2017)

should show me the following

2017-11-29

2017-11-30

2017-12-01

2017-12-02

2017-12-03

2017-12-04

2017-12-05

Thank you in advance for this space, I hope my question can be answered.

if(date == since || date == until){
            var cReg = Object.keys(regis[cc][date]).length;
            console.log(cReg);
            if (regis[cc].hasOwnProperty(date)) {
              html += "<div class='panel panel-default'>"+
                      "<div class='panel-heading'><a href=#"+cc+"-"+i+" data-toggle='collapse'>"+date+
                        "</a><span class='badge'>"+cReg+"</span></div>";
                        html += "<div class='panel-body collapse' id="+cc+"-"+i+" >hours";
            //console.log();
              for (var hour in regis[cc][date]) {
                if (regis[cc][date].hasOwnProperty(hour)) {
                  html += "<div>"+ regis[cc][date][hour] +"</div>"
                }
              }
            }
            html += "</div>";
            html += "</div>";
            i++;
       }else {
          console.log("la fecha no coincide");
        }
    
asked by YohaBR 28.12.2017 в 21:02
source

2 answers

1

You can do it using the library moments.js

var diasEntreFechas = function(desde, hasta) {
  	var dia_actual = desde;
    var fechas = [];
  	while (dia_actual.isSameOrBefore(hasta)) {
    	fechas.push(dia_actual.format('DD-MM-YYYY'));
   		dia_actual.add(1, 'days');
  	}
  	return fechas;
};

var desde = moment("2017-11-29");
var hasta = moment("2017-12-05");
var results = diasEntreFechas(desde, hasta);
console.log(results);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>
    
answered by 28.12.2017 / 21:22
source
2

With JavaScript native is very easy to get, you just need to know some methods that it provides to manipulate dates:

var fechaInicio = new Date('2017-12-20');
var fechaFin    = new Date('2017-12-28');

while(fechaFin.getTime() >= fechaInicio.getTime()){
    fechaInicio.setDate(fechaInicio.getDate() + 1);

    console.log(fechaInicio.getFullYear() + '/' + (fechaInicio.getMonth() + 1) + '/' + fechaInicio.getDate());
}
    
answered by 28.12.2017 в 21:25