views:

40

answers:

1

Hi,

I´m looking for a function/prototype extension, which returns all days of a given month. I couldn't find anything helpful at the standard date() functions (http://www.w3schools.com/jsref/jsref_obj_date.asp).

i.e.:

getDays(2010-05) // returns something like:

{ day:'Saturday', date:'2010-01-05' }, { day:'Sunday', date:'2010-02-05' }, { day:'Monday', date:'2010-03-05' }[...]

Just the day of the first day of the month may (saturday, 01) and the last (monday, 31) would be great, too.

+3  A: 

You can do this yourself pretty easily. The Javascript "Date" object is a great help for things like this:

function listDays(monthNumber) {
  var days = [],
    today = new Date(),
    d = new Date(today.getFullYear(), monthNumber, 1);

  while (d.getMonth() == monthNumber) {
    days.push({ "date": d.getDate(), "weekday": d.getDay()});
    d.setDate(d.getDate() + 1);
  }
  return days;
}

That only returns the days in the month numerically, but it'd be easy to extend with an array of day-of-week names:

function listDays(monthNumber) {
  var days = [],
    today = new Date(),
    d = new Date(today.getFullYear(), monthNumber, 1),
    weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

  while (d.getMonth() == monthNumber) {
    days.push({ "date": d.getDate(), "weekday": weekdays[d.getDay()]});
    d.setDate(d.getDate() + 1);
  }
  return days;
}
Pointy
Awesome! Thank you :)
koko
Remember that months are numbered 0 through 11, not 1 through 12!
Pointy
Yep, I already figured that out.
koko