Meandering Soul

This day is done, I'm going home.
eFranes Penguin Avatar

Get the number of days in a month with moment.js

09
Dec 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
var date = moment();
var currentMonth = date.month();

var daysInMonth = 31;         // Every month has 31 days
if (currentMonth % 2 == 1) {  // except every other month
    if (currentMonth == 1) {  // and then there is februrary
        if (date.isLeapYear()) { 
            daysInMonth = 29; // which has 29 if it's a leap year
        } else {
            daysInMonth = 28; // and 28 if it isn't.
        }
    } else {
        daysInMonth = 30;
    }
}
  • Published on December 09, 2015
  • 79 words