JavaScript: obtenga una matriz de nombres de días de una fecha dada (mes/año)

how can i get the whole month in day namesof given month/year? like :

var year = "2000";
var month = "7"

... some code here that makes an array with names of days of the given month...

and the output looks something like:

Array ("1. Sunday", "2. Monday", "3. Tuesday", ... "29. Thursday", "30. Friday", "31. Saturday");

Saludos cordiales, Chris

preguntado el 04 de julio de 12 a las 05:07

This isn't so much a "JavaScript" question as it is a "which date API do I want to use" question. The JavaScript part is easy as pie once you've found an API that will determine which days of the week fell in a particular month of a particular year. -

What output are you expecting for this date.Saturday? -

Scratch that... shouldn't be too hard at all... not thinking clearly this late :-) -

6 Respuestas

Esto debería hacer lo que pediste.

function getDaysArray(year, month) {
    var numDaysInMonth, daysInWeek, daysIndex, index, i, l, daysArray;

    numDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    daysIndex = { 'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6 };
    index = daysIndex[(new Date(year, month - 1, 1)).toString().split(' ')[0]];
    daysArray = [];

    for (i = 0, l = numDaysInMonth[month - 1]; i < l; i++) {
        daysArray.push((i + 1) + '. ' + daysInWeek[index++]);
        if (index == 7) index = 0;

    return daysArray;

Respondido 04 Jul 12, 05:07

If you want me to add comments to explain what I'm doing at each step, just let me know and I'll edit the post. - natlee75

Thank you very much! this is exactly what i was looking for. Thanks again! - Chris

h @natlee75 suppose that i want to assign "4 peole names" in every day of the month from people array extracted randomly what is the right path to achieve this? In other hand create an automagic woks shift for entire month from worker array. Thanks for possible answer - sundsx

Prueba esta

<!DOCTYPE html>

<p id="demo">Click the button to display todays day of the week.</p>

<button onclick="myFunction()">Try it</button>

<script type="text/javascript">
function daysInMonth(month,year) {
    return new Date(year, month, 0).getDate();

function myFunction()
{var b=[],weekday = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],month=7,year=2000;
for(var i=1,l=daysInMonth(month,year);i<l;i++){
var d = new Date(year,month-1,i);
console.log(b);}//b is the desired array


Respondido 04 Jul 12, 05:07

It should be noted that in your myFunction(), month is 1-based, not zero-based; that is the only way your implementation of daysInMonth will work properly. This may cause confusion as javascript's Date object uses a zero-based month value - jackwanders

Además, usando new Array para su weekday array is inefficient, you're better off doing weekday = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']. Also, there's no need to recreate the array in every iteration of your for círculo. - jackwanders

@Random - Invoking daysInMonth during each iteration of your for loop is highly inefficient. Something along the lines of for (var i = 1, l = daysInMonth(month, year); i < l; i++) is better because the code in the second "segment" of a for loop is invoked for every iteration through the loop. - natlee75

@Random - jackwanders is correct about the new Array call. It's strange that you used that there since you initialized b the proper way only a few lines above. Defining the whole array right from the start is the most efficient way, but even declaring an empty array and then pushing each day of the week into it would have been more efficient. - natlee75

FechaJS has the functions you need to implement the logic:

As @natlee75 noted, you can handle the logic from there.

Respondido 04 Jul 12, 05:07

You can do the logic yourself pretty easily using the standard JavaScript Date object. Here's an example of getting one date to push into your array. Just loop through the entire month calling the method. (jsviolín)

var daysOfTheWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

function getDisplay(month, day, year) {
    var d = new Date(year, month-1, day);
    return day + '. ' + daysOfTheWeek[d.getDay()];

array_push($dates, getDisplay(7, 4, 2012));

Respondido 04 Jul 12, 05:07

creo que debería haber month+1 - diEcho

No, month is zero based. January is zero. That's why I swapped it in that function to correspond with his variables.… - Scottheckel

that;s why if user input July (7) then java script should interpreted it as 6 - diEcho

Yeah, that's why I subtract 1 from the month in getDisplay(). - Scottheckel

Try this and see the result in console.

var weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
               'Thursday', 'Friday', 'Saturday'];
var month = '7';
var year = '2012';
realMonth = parseInt(month,10)-1;
var monthObj = {};
for(var i=1;i<30;i++)
    var d = new Date(year,realMonth,i);              
    monthObj[i] = weekday[d.getDay()];

Respondido 04 Jul 12, 05:07

I know this is an old question, but today a pregunta linked to this, and I'd like to point out one don't really need to do much things other than convert day (0-6) a string (Sunday-Saturday)

function getDays(year, month) { //month: 1-based, as normal person
    --month //change it to 0-based, as Date object
    let dayname = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
    let date = new Date(year, month)
    let result = []
      result.push(`${date.getDate()}. ${dayname[date.getDay()]}`)
    return result;


Respondido el 09 de diciembre de 19 a las 13:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.