javascript
  • jquery
  • json
  • date
  • object
  • 2014-10-07 2 views 0 likes 
    0

    hlp 저 아래의 객체에서 가장 가까운 날짜를 찾아야합니다. 가장 가까운 날짜는 시스템 날짜 + dayscounter와 비교하여 올 것입니다.자바 스크립트 배열 현재 날짜 + dayscounter가 가장 가까운 날짜를 찾습니다.

    제게 큰 도움이 될 수 있도록 도와주세요. 여기 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); 
    

    답변

    0

    같은 것에 대해 어떻게 :

    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/

    관련 문제