To know the last day of the month between 2 AngularJS dates

0

Greetings, I have a problem in my controller with AngularJS , I have an object that brings me the following

Objeto{
FechaInicio: "2017-11-04"
FechaTermino: "2017-03-04"
}

I need the way you tell me the last dates of the month that exist in that time interval and I create a new new list that should look something like this:

nuevaLista {
fechaFin[0]: 2017-11-30, //(30 dias)
fechaFin[1]: 2017-12-31,  //(31 días)
fechaFin[2]: 2018-01-01,  //(31 días)
fechaFin[3]: 2018-02-28,  //(28 días o 29 si aplica)
}

I'm reviewing the library moment (moment().endOf(String);) but I do not know how to do it so that it calculates only in that interval.

    
asked by Paola Godoy 06.12.2017 в 15:04
source

3 answers

1

last day of the month on a date

var fecha = new Date(f.anofin1.value, f.mesfin1.value, 0).getDate();

Example: date = 2018-07-15;

var fecha = new Date(2018, 07, 0).getDate();

Result: date = 31;

    
answered by 16.11.2018 в 22:16
0

You can do it only with Javascript, you can have a variation the answer

let fechaInicio = "2017-11-04";
function ultimoDiaMes(fecha){
  let arrayFecha = fecha.split('-');
  let fechaUltimo = new Date(arrayFecha[0], arrayFecha[1]) 
  fechaUltimo.setDate(fechaUltimo.getDate() - 1)

  return fechaUltimo.toLocaleString()
} 
console.log(ultimoDiaMes(fechaInicio));
    
answered by 06.12.2017 в 15:31
0

I did it with the momment library

  let fechafinMes = moment(fechaaCalcular).endOf('month');
    
answered by 23.12.2017 в 13:24