2012-10-30 46 views
9

有一種方法可以擁有一個月或一年的所有日子嗎?我正在尋找這個爲了禁用日期選擇器中的某些特定日子,我將在後臺有一個頁面來選擇這些日子禁用。使用Date對象查找一個月中的所有日期?

因此,我需要在一個月內顯示所有日子,並在每天下方添加「激活或停用」按鈕。有沒有辦法用Date對象找到這些日子?我發現這個鏈接,例如:Displaying all the days of a month但我真的不明白它,再加上它是Java,我正在嘗試在JavaScript中找到解決方案。

謝謝您的幫助

+0

「這些日子」或「有這些日子」的真正含義是什麼?目標不明確 – charlietfl

回答

37

要獲取一個月中所有日子的列表,您可以在一個月的第一天以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; 
} 

http://jsfiddle.net/kKj8h/1/

+0

非常感謝Juan,這正是我需要的!對代碼date.setDate(date.getDate()+ 1)的輕微更正 – Paul

+0

;應該在date.push數組調用前 – Timothy

+0

@Timothy這不起作用的測試用例是什麼? –

4

我不確定你的描述是否標準禁用日期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]); 

使用任何你想用它日子裏,你幾乎可以做的數組,知道的一天一週。

+0

感謝Jeremy的回答! – Paul

1

下面是通過每個月運行,以確定該月的最後一天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>') 
} 

DEMO:http://jsfiddle.net/5k8sn/1/

+0

謝謝你的答案charlietfl! – Paul

1

我已經實現了您使用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

+0

非常感謝布魯諾,這正是我一直在尋找的! – Paul

+0

@Juan Mendes我不確定明白你的意思,如果我們不使用indexOf,我們應該怎麼做? – Paul

+0

@Juan Mendes,謝謝 – Paul

相關問題