2012-12-04 84 views
1

例:今天是4月查找元素最接近的日期,然後添加類

  1. 2012年12月

  2. 2012年12月

月標題(2012年12月5日)

標題(7。 2012年12月)

  1. 2012年12月

最接近的日期是12月5日(不3.December)(更新不早)

而且如果人數超過一 「12月5日」,所以添加類只有第一胎

HTML:

<div class="wrap"> 

<div class="zone" id="one"> 
<div class="box"> 
    <footer class="time">1. December 2012</footer> 
</div> 


<div class="box"> 
    <footer class="time">1. December 2012</footer> 
</div> 

<div class="box"> 
    <footer class="time">3. December 2012</footer> 
</div> 


<div class="box"> 
    <h2>Title <span class="time">(5. December 2012)</span></h2> 
</div> 


<div class="box"> 
    <h2>Title <span class="time">(7. December 2012)</span></h2> 
</div> 


<div class="box"> 
    <footer class="time">9. December 2012</footer> 
</div> 

</div> 

<div class="zone" id="two"> 
    <!-- Same .zone#one but i will focus for .zone#one only--> 
</div> 

</div> 


<code></code> 

的jQuery:

var closest = []; 

$('.wrap > .zone:eq(0) .box').each(function(i) { 
     var date = $(this).find(".time").html().replace("(","").split("."); 
     closest.push(date[0]); 
}); 



$("code").html(closest+""); 

遊樂場:http://jsfiddle.net/WJvZb/

我走到現在這一步,但也沒辦法找到最接近的日期,並添加類它(例如。 .closest class)

+1

我想你想要做的是這裏計算的日期,你會得到一個整數回以毫秒爲單位,你計算每一個日期,每次它比小舊的你替換它。你會替換第一個ofc,只要檢查你的當前值是否爲0(或-1是否安全),如果它大於你剛剛計算的值。 –

回答

1

您應該用英文命名日期,因此「desember」...此外,您可以遍歷日期並從中創建Date對象,然後找到最接近的就很容易。

var closest = []; 

$('.wrap > .zone:eq(0) .box').each(function(i) { 
    var date = $(this).find(".time").html().replace("(","").replace(")",""); 
     closest.push(new Date(date)); 
}); 

function closestTime(days, testDate) 
{ 
    var bestDiff = null; 

    for(i = 0; i < days.length; ++i){ 
     currDiff = Math.abs(days[i] - testDate); 
     if(currDiff < bestDiff || bestDiff == null){ 
      bestDate = days[i]; 
      bestDiff = currDiff; 
     } else if (currDiff == bestDiff && days[i] > testDate) { 
      bestDiff = currDiff;    
     } 
    } 

    return bestDate; 
} 

console.log(closestTime(closest,new Date())); 
+0

有'日期{無效日期}' – l2aelba

+0

哦,秒給我時間 – l2aelba

+0

得到最接近的數組中的日期:(http://jsfiddle.net/WJvZb/1/ – l2aelba

1

這樣做的最簡單的方法是解析每個日期時間(取決於格式),將它們推入數組,然後循環數組計算它們之間的時間差,並拉出最小值。你可以使用一些像moment.js這樣有用的庫來進行日期分析和計算。

var now = new Date(); 
var closest = -1; //array ndx 
var times = []; 
var els = []; 

$('.time').each(function(i) { 
    times.push(someParsingMethod($(this).text()); 
    els.push($(this)); 
}); 

//initial difference 
var diff = times[0].getTime() - now.getTime(); 

//check for lowest difference greater with a target date greater than now 
for(var i=0;i<times.length;i++){ 
    var tmpDiff = times[i].getTime() - now.getTime(); 
    if(times[i].getTime() > now.getTime() && tmpDiff < diff){ 
     closest = i; 
    } 
} 

if(closest != -1){ 
    els[closest].addClass("closestClass"); 
} 

這只是僞代碼,真正的挑戰是解析專有的日期格式。

+0

你可以試試這個嗎? HTTP://的jsfiddle。net/WJvZb /:D非常感謝 – l2aelba

+1

如果不爲您編寫解析方法,或者使用像momentjs這樣的庫,我無法真正給你一個小提琴。嘗試爲自己發現那部分將會對你有益。 –

+0

好吧,謝謝,無論如何,我會嘗試 – l2aelba

1

只要你使用數組這是實現它的最簡單的方法。

var closest = []; 
// Current Date 
var current = new Date("04/12/2012"); 
// Function to get the Minimam value in Array 
Array.min = function(array){ 
    return Math.min.apply(Math, array); 
}; 
// Played on your code 
$('.wrap > .zone:eq(0) .box').each(function(i) { 
    var date = $(this).find(".time").html().replace("(","").split("."); 
    // If higher than current date 
    if(current.getDay() < date[0]) { 
     closest.push(date[0]); 
    } 
}); 
// Get the closest day.. 
$("code").html(Array.min(closest)+""); 

上播放您的演示:http://jsfiddle.net/BerkerYuceer/WJvZb/40/

1
function findActive(days, testDate) { 
     var bestDiff = 0; 
     var bestDate = null; 
     var strDate = "No active"; 

     for(i = 0; i < days.length; ++i){ 
      var dDate = getDate(days[i]); 

      currDiff = Math.abs(dDate - testDate); 

      if(i ==0){ 
       bestDiff = currDiff; 
      } 

      if(dDate <= testDate){ 
       if(currDiff < bestDiff){ 
        strDate = days[i]; 
        bestDate = dDate; 
        bestDiff = currDiff; 
        }else if(currDiff == bestDiff){ 
        strDate = days[i]; 
        bestDate = dDate; 
       } 
      } 
     } 
     return strDate; 
    } 


function getDate(strDate){ 
    var day = strDate.substring(0,2); 
    var month = strDate.substring(3,5); 
    var year = strDate.substring(6,10); 

    return new Date(parseInt(year), parseInt(month) - 1, parseInt(day)); 
} 

date mask is: dd.MM.yyyy 

     this code find active date. 
     enter code here 
     example 
     today is: 07.06.2013 

     dates is: 03.06.2013, 4.6.2013, 5.6.2013 => return 5.6.2013 
     dates is: 03.06.2013, 14.6.2013, 15.6.2013 => return 3.6.2013 
     dates is: 08.06.2013, 14.6.2013, 15.6.2013 => return null 
     dates is: 08.06.2013, 7.6.2013, 9.6.2013 => return 7.6.2013 
     etc