0
How to implement an algorithm that lists every day of a given month using Javascript?
Because I have a combobox with the months and I need to popular this combo of days according to the selected month.
0
How to implement an algorithm that lists every day of a given month using Javascript?
Because I have a combobox with the months and I need to popular this combo of days according to the selected month.
3
I implemented something very fast for you to have an idea, from this you can popular your select of Year/Month, the function receives the month and the year. In this example I used the month of December 2015.
window.onload = function() {
var select = document.getElementById("dias");
var options = getDiasMes(12, 2015);
for (var i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
}
function getDiasMes(month, year) {
month--;
var date = new Date(year, month, 1);
var days = [];
while (date.getMonth() === month) {
days.push(date.getDate());
date.setDate(date.getDate() + 1);
}
return days;
}
Dia: <select id="dias"></select>
Thanks solved my problem :D
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
You’ve done something so far?
– PauloHDSousa
What part of the task is causing you difficulty? After extracting what is the month of
input
, all you need to do is check how many days are this month (depending on the format - in number, string, spelled - it may be easier to store these values in an object) and generate the list that goes from1
up to that number of days. Unless you are referring to a specific year, which may or may not be leap, etc. Please add more details to the question by clarifying these points and showing what you have tried to do.– mgibsonbr