2013-06-04 186 views
2

如果這是重複的,但我找不到與我的問題相匹配的現有答案(我看了!)。將今天的日期與日期列表進行比較

我有一個列表,每個都有一個<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)* 

,並沒有什麼表示。

你能幫忙嗎,我以爲這會挺直的,但不能得到結果?

+3

你打算如何處理年終邊緣狀態? 12/31小於1/1,對不對?您需要使用完整的Date對象才能使用比較運算符。 – Tap

+0

一個奇妙的迴應!謝謝大家非常感謝。剛剛花了幾個小時嘗試每一個,(更多的是學習的原因比任何東西!)關鍵是使用'each'的循環。 'passionateCoder對.closest的使用對我來說是新的,並且保存了'養育子女',所以我不得不使用它,假設不包括今天的事件可以修復。 – Neil

+0

passionCoder在我輸入最後一條評論時發佈了'修復'。所有排序和巡航。 – Neil

回答

1

正如評論所說,它總是更好地對比Date類型(變量,讓你neednt需要長達十二月廣泛的比較照顧,這可能很麻煩)。所以,我用的方法是將文本轉換爲Date類型的對象,並將其與當前日期比較:由

var d = new Date(); 
    d.setHours(0, 0, 0, 0); 
    var year = d.getFullYear(); 
    $(".chkdate").each(function (i) { 
     //added a setHours function to make time parameters of Date zero before comparing 
    ((new Date($(this).text() + "/" + year)).setHours(0, 0, 0, 0) < d) ? $(this).closest("li").hide() : $(this).closest("li").show(); 
    }); 

假設:

編輯

  • 只好添加一些行,因爲它是時間比較當前日期以及使didnt匹配。從參數中刪除時間參數使用.setHours(0, 0, 0, 0);
  • 改變><
+1

這似乎是最光鮮的解決方案,不得不改變>以<隱藏'舊日期'(沒問題),但爲什麼它不顯示今天的項目? – Neil

+0

編輯答案&小提琴..因爲這個代碼變得稍大:( – krishgopinath

+0

這是做了詭計,非常讚賞。現在玩這個,直到我打破它!謝謝您抽出時間來幫助。 – Neil

1

你需要一個for循環:

var datestocheck = $('#whatson li span.chkdate'); 

    for (var i = 0; i < datestocheck.length; i++) { 
    var compare = parseDate(datestocheck[i].text()); 

    if (compare < todaysdate) { 
     datestocheck[i].hide(); 
    } 
    } 
+0

和東西來比較你的日期:http://stackoverflow.com/questions/492994/compare-dates-with-javascript – jammykam

+1

順便說一句:你應該考慮添加一個'datetime'屬性到你的項目,如'datetime =「YYYY-MM -DD「' – Jonathan

+0

@jammykam確實,在代碼中添加了parseDate()... – Jonathan

0

請看看http://jsfiddle.net/2dJAN/57/

$(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; 
    $('#whatson li').each(function(){ 
     var datetocheck = $(this).text(); 
     if (datetocheck < todaysdate) { 
      $(this).hide(); 
      }   
    }); 
}) 

可我知道這是你的要求或沒有。

+0

我錯過了'each'來循環它,謝謝! – Neil

+0

這也似乎只是隱藏日期,而不是(父母)李,即整個文本。 – Neil

1

Example on jsFiddle

示例HTML

<ul id="whatson"> 
    <li> 
    <span class="chkdate">06/01</span> 
    somestuff1 
    </li> 
    <li> 
    <span class="chkdate">06/04</span> 
    somestuff1 
    </li> 
    <li> 
    <span class="chkdate">06/05</span> 
    somestuff2 
    </li> 
</ul> 

腳本

var today = new Date(); 
$("#whatson .chkdate").each(function() { 
    // split the text and get the month/day value 
    var datepieces = $(this).text().split("/"); 
    var month = datepieces[0]; 
    var day = datepieces[1]; 

    // if the month of the element is previous to today's month 
    // hide the li 
    if (month < (today.getMonth() + 1)) 
     $(this).closest("li").hide(); 

    // if the day of the element is previous to today 
    // hide the li 
    if (day < today.getDate()) 
     $(this).closest("li").hide(); 
}); 
+0

是否有分兩個階段進行檢查的好處,而不僅僅是一個月? – Neil

1

jsFiddle Example

通過使用所選擇的每個元件的.each() jQuery函數來循環。使用JavaScript .split()函數將文本值轉換爲日期。另請參閱documentation on JavaScript dates

HTML:

<ul id="whatson"> 
    <li><span class="chkdate">06/03</span> somestuff1</li> 
    <li><span class="chkdate">06/04</span> somestuff1</li> 
    <li><span class="chkdate">06/05</span> somestuff2</li> 
</ul> 

的JavaScript:

$(document).ready(function() { 
    var todaysdate = new Date(); 

    var datestocheck = $('#whatson li').each(function (i, v) { 
     var listitem = $(v); var chkdate = $('span', listitem); 

     var stringdate = chkdate.text(); 
     var monthtocheck = stringdate.split('/')[0]; 
     var daytocheck = stringdate.split('/')[1]; 

     var datetocheck = new Date(); 
     datetocheck.setFullYear(
      todaysdate.getFullYear(), // four digit year 
      monthtocheck-1, // month number minus 1 b/c month is 0-11 
      daytocheck // the day number 
     ); 

     //alert(datetocheck + ' < ' + todaysdate); 

     if (datetocheck < todaysdate) { 
      listitem.hide(); 
     } else { 
      listitem.show(); 
     } 
    }); 
}) 
相關問題