0

Just like jquery datepicker with showOtherMonths: true, how to get the list of the date in a month including the dates of other months?

I was thinking to make 7 columns(Sun-Sat) and 6 rows calendar, so the date array should contain 42 list. Any help/hint would be grateful.

Ardeus
  • 1,921
  • 3
  • 17
  • 26

1 Answers1

0

I would like to share what I have tried so far. Not exactly always 7x6 grid calendar, but I think my solution should do for me right now.

I obtained the first two functions from this answer Link1 Link2

$(function(){
 /** 
 * @param {int} month 0-11 based
 * @param {int} year 4 digits based
 * @returns {int} number of days
 */
function daysInMonth(month, year) {
    return new Date(year, month + 1, 0).getDate(); //adding 1 month, and zero day will be the previous month's last day
}

/**
 * @param {int} The month number, 0 based
 * @param {int} The year, not zero based, required to account for leap years
 * @return {Date[]} List with date objects for each day of the month
 */
function getDaysInMonth(month, year) {
    var date = new Date(year, month, 1);
    var days = [];
    while (date.getMonth() === month) {
        days.push(new Date(date));
        date.setDate(date.getDate() + 1);
    }
    return days;
}
 /**
 * @param {int} currentMonth 0-11 
 * @param {int} currentYear 4 digit year
 * @return {Date[]} List with date objects of previous other month
 */
function getDatePrevOtherMonth(currentMonth, currentYear) {
    var indexCurrentFirstDay = new Date(currentYear, currentMonth, 1).getDay();
//console.log(indexCurrentFirstDay);
    var pYear = "";
    var pMonth = "";
    var pDate = [];
    if (currentMonth === 0) {
        pYear = currentYear - 1;
        pMonth = 11;
    } else {
        pMonth = currentMonth - 1;
        pYear = currentYear;
    }
    var totalDaysOfPreviousMonth = daysInMonth(pMonth, pYear);
    if (indexCurrentFirstDay > 0) {
        var prevMonthDayStart = totalDaysOfPreviousMonth - (indexCurrentFirstDay - 1);       //-1 to ensure inclusive day
     //    console.log(prevMonthDayStart);
        for (var i = prevMonthDayStart; i <= totalDaysOfPreviousMonth; i++) {

            pDate.push(new Date(pYear, pMonth, i));
  //console.log(pYear + "-" + pMonth + "-" + i);
        }
    }

    return pDate;
}

/**
 * 
 * @param {int} currentMonth 0-11 
 * @param {int} currentYear 4 digit year
 * @return {Date[]} List with date objects of next other month
 */
function getDateNextOtherMonth(currentMonth, currentYear) {
    var totalDaysOfCurrentMonth = daysInMonth(currentMonth, currentYear);
    var indexCurrentLastDay = new Date(currentYear, currentMonth, totalDaysOfCurrentMonth).getDay();
  //console.log(indexCurrentLastDay);
    var nYear = "";
    var nMonth = "";
    var nDate = [];
    if (currentMonth === 11) {
        nMonth = 0;
        nYear = currentYear + 1;
    } else {
        nMonth = currentMonth + 1;
        nYear = currentYear;
    }

    if (indexCurrentLastDay < 6) {

        var nextMonthDayToTake = 6 - indexCurrentLastDay;
 //    console.log(nextMonthDayToTake);
        for (var i = 1; i <= nextMonthDayToTake; i++) {
            nDate.push(new Date(nYear, nMonth, i));
   //console.log(nYear + "-" + nMonth + "-" + i);
        }
    }

    return nDate;
}

var indexMonth= 10; //Nov
var fullYear=2013;
 var datePrevOtherMonth = getDatePrevOtherMonth(indexMonth, fullYear);
    var dateNextOtherMonth = getDateNextOtherMonth(indexMonth, fullYear);
    var dateCurrentMonth = getDaysInMonth(indexMonth, fullYear);
    var dateCalendarMonth = $.merge(datePrevOtherMonth, dateCurrentMonth);
    dateCalendarMonth = $.merge(dateCalendarMonth, dateNextOtherMonth);
$.each(dateCalendarMonth, function(index,value){
   var calendarMonth= value.getMonth()+1;   
   console.log(value.getFullYear()+"-"+calendarMonth+"-"+value.getDate());

 });
});

http://jsfiddle.net/dxtNT/

Community
  • 1
  • 1
Ardeus
  • 1,921
  • 3
  • 17
  • 26