如果這是重複的,但我找不到與我的問題相匹配的現有答案(我看了!)。將今天的日期與日期列表進行比較
我有一個列表,每個都有一個<span>*fixed date*</span>
。
包含日期列表看起來像這樣....
<ul id="whatson">
<li>
<span class="chkdate">06/04</span>
somestuff1
</li>
<li>
<span class="chkdate">06/05</span>
somestuff2
</li>
.......
</ul>
我想過濾列表這麼大年紀的所有日期比現在被隱藏。
會給我今天的日期,例如mm/dd = 06/04,看起來不錯。
我現在要檢查每個列表項目,例如格式爲。 06/04和隱藏那些比今天更早的項目。
它只需要在頁面加載時運行一次。
到目前爲止我的代碼...
$(document).ready(function() {
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var todaysdate = (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
alert("checking " + datetocheck + " against today = " + todaysdate);
--- * temp check *
var datetocheck = $('#whatson li span.chkdate').text();
if (datetocheck < todaysdate) {
$('#whatson li').hide();
} else {
$('#whatson li').show();
};
})
我的警報顯示..
Checking 06/0406/04 against today 06/04 *(when all list dates match)*
,並正確顯示。
改變一個或多個日期清單警報顯示
Checking 06/0306/04 against today 06/04 *(when one or more list dates don't match)*
,並沒有什麼表示。
你能幫忙嗎,我以爲這會挺直的,但不能得到結果?
你打算如何處理年終邊緣狀態? 12/31小於1/1,對不對?您需要使用完整的Date對象才能使用比較運算符。 – Tap
一個奇妙的迴應!謝謝大家非常感謝。剛剛花了幾個小時嘗試每一個,(更多的是學習的原因比任何東西!)關鍵是使用'each'的循環。 'passionateCoder對.closest的使用對我來說是新的,並且保存了'養育子女',所以我不得不使用它,假設不包括今天的事件可以修復。 – Neil
passionCoder在我輸入最後一條評論時發佈了'修復'。所有排序和巡航。 – Neil