0

Can you please take a look at this demo and let me know how I can dynamically populate the number of each month for current year in jQuery or JavaScript?

var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var daysInMonth = [];
var d = new Date();
var n = d.getMonth();

for (var i = 0; i < monthNames.length; i++) {
    daysInMonth.push(d.getMonth());
}
console.log(daysInMonth);
4
  • What output do you want? [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] ? Commented Oct 16, 2014 at 3:34
  • Hi Sam, the number of days in each month Commented Oct 16, 2014 at 3:34
  • So like: October = 31? Commented Oct 16, 2014 at 3:40
  • yeah so for example for 2014 would be an array of [31, 28, 31, 30, 31, 30, 31, ... ] Commented Oct 16, 2014 at 3:46

2 Answers 2

1
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var daysInMonth = [];

for (var i = 0; i < monthNames.length; i++) {
    var year = 2014;
    var month = new Date(monthNames[i] + " 01 "+ year).getMonth() + 1;
    daysInMonth.push(new Date(year, month, 0).getDate());
}
console.log(daysInMonth);

DEMO http://jsfiddle.net/0mj2cxmt/7/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks sam this is exactly what I was looking for but there are some issue here! Feb 2014 has 28 days in but you are getting 31!
I updated the code, the getMonth() function returns 0 based month number so I forgot to add 1 to that value.
1

@Sam Battat's answer works too, but for a simple snippet of code that works on any year when called you could try this:

var thisDay = new Date();
var thisYear = thisDay.getYear();
var feb29th = new Date(thisYear, 1, 29);
var febDays = ((feb29th.getMonth() === 1) ? 29 : 28);
var dayCounts = [31,febDays,31,30,31,30,31,31,30,31,30,31];

Notes:

  • The number of days is hard coded for all months except February since they don't change
  • The feb29th variable above will actually become March 1st on years that don't have 29 days (e.g. non-leap years) and thus the month won't be "1"... defaulting the number of days back to 28

Update:

After running this perf test http://jsperf.com/leap-year-check it has become apparent that the "crafty" check for a leap year performance is nowhere near as good as basic math checks.

Thus I'd consider this to be even more efficient.

var thisDay = new Date();
var thisYear = thisDay.getYear();
var febDays = 28;
if((thisYear % 4 == 0) && (thisYear % 100 != 0) || (thisYear % 400 == 0)){
  febDays = 29;
}
var dayCounts = [31,febDays,31,30,31,30,31,31,30,31,30,31];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.