有一種方法可以擁有一個月或一年的所有日子嗎?我正在尋找這個爲了禁用日期選擇器中的某些特定日子,我將在後臺有一個頁面來選擇這些日子禁用。使用Date對象查找一個月中的所有日期?
因此,我需要在一個月內顯示所有日子,並在每天下方添加「激活或停用」按鈕。有沒有辦法用Date對象找到這些日子?我發現這個鏈接,例如:Displaying all the days of a month但我真的不明白它,再加上它是Java,我正在嘗試在JavaScript中找到解決方案。
謝謝您的幫助
有一種方法可以擁有一個月或一年的所有日子嗎?我正在尋找這個爲了禁用日期選擇器中的某些特定日子,我將在後臺有一個頁面來選擇這些日子禁用。使用Date對象查找一個月中的所有日期?
因此,我需要在一個月內顯示所有日子,並在每天下方添加「激活或停用」按鈕。有沒有辦法用Date對象找到這些日子?我發現這個鏈接,例如:Displaying all the days of a month但我真的不明白它,再加上它是Java,我正在嘗試在JavaScript中找到解決方案。
謝謝您的幫助
要獲取一個月中所有日子的列表,您可以在一個月的第一天以Date
開始,增加一天,直到月份發生變化。
/**
* @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;
}
要在日期選擇器禁用日期,您可以使用這裏描述的答案:
https://stackoverflow.com/a/12061715/48082
要選擇多個日期(如後臺辦公應用程序),你可以使用這個插件:
http://multidatespickr.sourceforge.net/
感謝Cheeso的回答 – Paul
我不確定你的描述是否標準禁用日期datepicker將與你一起工作,所以我會直接回答你的問題。
您可以通過這樣構建了一個月天的數組相當容易:
var numOfDays = new Date(2012, 10, 0).getDate(); //use 0 here and the actual month
var days = new Array();
//This will construct an array with all the elements represent the day of the week
//(i.e. Oct 30th would be days[30-1] or days[29]) and the value would be the actual
//day of the week (i.e. Tuesday which is representing by the number 2)
for(var i=0;i<=numOfDays;i++)
{
days[i] = new Date(2012,9,i+1).getDay(); //use month-1 here
}
//This will give you a number from 0 - 6 which represents (Sunday - Saturday)
alert(days[29]);
使用任何你想用它日子裏,你幾乎可以做的數組,知道的一天一週。
感謝Jeremy的回答! – Paul
下面是通過每個月運行,以確定該月的最後一天loopp。 Javascript日期對象索引月份從零開始,如果您將日期設置爲零,它將恢復到前一個月的最後一天。手持確定閏年最後一天二月
Date(2012, 12, 0)
將回到2012年12月31日
Date (2012,0,0)
將返回十二月31,2011
和所有重要的一個要弄清楚是2月與
Date (2012,3,0)
返回2月29日今年閏年
var mos=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']
for (i = 0; i < 12; i++) {
var lastDate = new Date(2012, i+1, 0);
$('body').append('Last day of ' + mos[i] + ' is ' + lastDate.getDate()+'<br>')
}
謝謝你的答案charlietfl! – Paul
我已經實現了您使用jQuery datepicker請求的功能。
首先,添加在後臺選擇要被關閉的所有日期到一個數組
// format yyyy-mm-dd
var disabledDates = [
"2012-10-01",
"2012-10-02",
"2012-10-30",
"2012-09-12"
];
其次,指定具有兩個功能
$("#datepicker").datepicker({
// only enable date if dateEnabled returns [true]
beforeShowDay: dateEnabled,
// once a date has been selected call dateSelected
onSelect: dateSelected
});
這裏日期選擇器是所需的定義功能
function dateEnabled(d) {
// if date found in array disable date
if(disabledDates.indexOf(getDate(d)) > -1) {
return [false];
} else {
return [true] ;
}
}
將日期轉換爲字符串以便與日期進行比較在陣列
function getDate(d) {
var day,
month,
year;
day = d.getDate();
month = d.getMonth() + 1; // add 1 as getMonth returns 0 - 11
year = d.getFullYear();
if(month < 10) month = "0" + month;
if(day < 10) day = "0" + day;
return year + "-" + month + "-" + day;
}
一旦日期已經選定過程中它
function dateSelected(d) {
// do stuff with string representation of date
}
這裏是一個小提琴http://jsfiddle.net/KYzaR/7/
只是認爲這將是值得一提的是Array.indexOf最近除了ECMA -262標準,所以在IE7和IE8的情況下不支持。以下MDN頁面提供了實現這些瀏覽器的Array.indexOf的代碼。https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf
「這些日子」或「有這些日子」的真正含義是什麼?目標不明確 – charlietfl