简体   繁体   English

在元素中查找最接近的日期然后添加类

[英]Find closest date in elements then add class

Ex : Today is 4. Dec 例如:今天是4月12日

  1. December 2012 2012年12月

  2. December 2012 2012年12月

  3. December 2012 2012年12月

Title (5. December 2012) 标题(2012年12月5日)

Title (7. December 2012) 标题(2012年12月7日)

  1. December 2012 2012年12月

closest date is 5. December (not 3.December) (newer not older) 最近的日期是5月12日(不是12月)(更新不是更老)

And If morethan one "5. December" so add class only first-child 如果超过一个“十二月五日”那么只增加班级的第一个孩子

HTML : 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 : 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+"");

Playground : http://jsfiddle.net/WJvZb/ 游乐场: http //jsfiddle.net/WJvZb/

I come to this step now , but have no idea to find closest date and add class it (ex. .closest class) 我现在来到这一步,但不知道找到最近的日期并添加类(例如.closest类)

The most foolproof way of doing this is to parse each date time (depends on the format), push them into an array, then loop over array calculating time difference between them, and pull the smallest value. 最简单的方法是解析每个日期时间(取决于格式),将它们推入一个数组,然后循环数组计算它们之间的时间差,并拉出最小值。 You can use some useful libraries like moment.js to do date parsing and calculations. 您可以使用一些有用的库(如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");
}

It's just pseudo code, the real challenge in this task is parsing the proprietary date formats. 它只是伪代码,这项任务的真正挑战是解析专有日期格式。

You should name your dates in english hence "desember"... Also then you could iterate through your dates and create Date objects from it, then find closest is easy. 你应该用英语命名你的日期,因此“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()));

as long as you are using array this is the easiest way to achive it. 只要您使用数组,这是实现它的最简单方法。

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)+"");

played on your demo : http://jsfiddle.net/BerkerYuceer/WJvZb/40/ 在您的演示中播放: http//jsfiddle.net/BerkerYuceer/WJvZb/40/

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM