就像带有showOtherMonths: true
的jquery datepicker一样,如何获取一个月中的日期列表,包括其他月份的日期?
我正在考虑制作7列(Sun-Sat)和6行日历,因此日期数组应该包含42个列表。任何帮助/提示都将不胜感激。
答案 0 :(得分:0)
我想分享到目前为止我尝试过的内容。并不总是7x6网格日历,但我认为我的解决方案现在应该为我做。
$(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());
});
});