Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: get array of day names of given date (month/year)

Tags:

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");

Best regards, Chris

like image 752
Chris Avatar asked Jul 04 '12 04:07

Chris


1 Answers

This should do what you asked.

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;
}
like image 125
natlee75 Avatar answered Sep 20 '22 10:09

natlee75