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.
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.
function diasNoMes(mes, ano) {
var data = new Date(ano, mes, 0);
return data.getDate();
}
// Exemplo:
console.log(diasNoMes(2, 2017)); // Exibe 28.
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());
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());