【问题标题】:javascript array find nearest date with current date + dayscounterjavascript数组查找与当前日期最近的日期+天数计数器
【发布时间】:2014-10-07 15:03:15
【问题描述】:

请帮助我,我必须从下面的对象中找到最近的日期,最近的日期将与系统日期 + 天数计数器相比。

请帮忙,这对我很有帮助。这是 jsFiddle 链接: http://jsfiddle.net/g7rsp4b2/1/

<div class="component" data-datejson='{"success":{"yrdateslist":["October 10,2014","October 18,2014","October 22,2014","October 09,2014","October 21,2014","October 22,2014"],"dayscounter":"8"}}' id="onCampusDate">

</div>

var allDates = $('.component').data('datejson'); 
var dates = allDates.success.yrdateslist;
var dayscounter = allDates.success.dayscounter;
var now = new Date();
var newDates = [];
for (var i = 0; i < dates.length; i++) {
    newDates.push(dates[i]);
}

alert(newDates);
alert(now);

【问题讨论】:

    标签: javascript jquery json date object


    【解决方案1】:

    怎么样:

    var allDates = $('.component').data('datejson'); 
    var dates = allDates.success.yrdateslist;
    var dayscounter = allDates.success.dayscounter;
    var now = new Date();
    
    //Add the days counter to now
    now.setDate(now.getDate() + dayscounter);
    
    //This is the key -> Sort by the difference in time
    dates.sort(function(a,b){
        var nowTime = now.getTime(),
            atime = Math.abs(atime - nowTime),
            btime = Math.abs(btime- nowTime);
    
        return Math.abs(atime - btime); 
    });
    

    那么你的截止日期应该是dates[0];

    见:http://jsfiddle.net/g8ezcoz7/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 2012-08-01
      • 2012-02-03
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      相关资源
      最近更新 更多