How many days does the current month have with Javascript?

1

I would like to know how many days the current month has with Javascript, for example with PHP, I would do so:

Date('t');

Very simple with PHP, but what about Javascript with some easy way?

Obs. I use AngularJS if it helps in anything.

    
asked by anonymous 08.02.2017 / 23:56

3 answers

2
function diasNoMes(mes, ano) {
    var data = new Date(ano, mes, 0);
    return data.getDate();
}

// Exemplo:
console.log(diasNoMes(2, 2017)); // Exibe 28.
    
09.02.2017 / 00:10
3

function _numDias(){
  var objData = new Date(),
      numAno = objData.getFullYear(),
      numMes = objData.getMonth()+1,
      numDias = new Date(numAno, numMes, 0).getDate();

  return numDias;
}

console.log(_numDias());
    
09.02.2017 / 00:41
2

Returns the number of days in the current month. This month of February 2017 returns 28. Month of March 31 and so on. Bisexual feasts will return 29.

Date.prototype.diasNoCorrenteMes = function() {
  var days = [30, 31],
  m = this.getMonth();

  if (m == 1) {
    return ( (this.getFullYear() % 4 == 0) && ( (this.getFullYear() % 100 != 0 ) || (this.getFullYear() % 400 == 0) ) ) ? 29 : 28;
  } else {
    return days[(m + (m < 7 ? 1 : 0)) % 2];
  }
}

var myDate = new Date();

console.log(myDate.diasNoCorrenteMes());
    
09.02.2017 / 00:21