2017-02-12 3 views
0

요일을 기준으로 테이블 행을 표시하거나 숨기고 싶습니다.) moment.format에서 'DDDD'로Moment JS 날짜 범위

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 



<button type="button" id="button1">Monday</button> 
<button type="button" id="button2">Tuesday</button> 

<table id="myTable"> 
<thead> 
    <tr> 
    <th>Table</th> 
    </tr> 
</thead> 

<tbody> 
    <tr>   
    <td class="dates">13/02/2017 12:45 pm</td>  
    </tr> 

    <tr> 
    <td class="dates">14/02/2017 12:45 pm</td>  
    </tr> 

</tbody> 


$('#button1').click(function(){ 
    var $dates = $('.dates'); 
    var m = moment().add(1, 'd'); 

$dates.each(function() { 
    var date = moment($(this).text(), 'DD/MM/YYYY hh:mm a'); 
    if (date.isBetween(moment(), m)) { 
     $(this).parent().show(); 
    } else { 
     $(this).parent().hide(); 
    } 
    }); 
}); 

답변

0

(쉽게 비교할 수 요일 이름을 나타냅니다 : 나는 순간이 코드는 가죽이 아니라 일보다, 24 시간의 기간을 기준으로/쇼 버튼 레이블.

모든 버튼에 공통 클래스를 추가하여 클릭 핸들러를 연결합니다. jQuery를 사용하면 선택 기준과 일치하는 모든 요소를 ​​선택할 수 있습니다.

핸들러에 들어가면 클릭 한 버튼의 라벨을 가져와 파싱 된 날짜와 비교하여 각각 표시/숨기기를 결정합니다.

$('.day-btns').click(function() { 
 
    var btnText = $(this).text(); 
 
    $('.dates').each(function() { 
 
    var date = moment($(this).text(), 'DD/MM/YYYY hh:mm a'); 
 
    if (date.format('dddd') == btnText) { 
 
     $(this).parent().show(); 
 
    } else { 
 
     $(this).parent().hide(); 
 
    } 
 
    }); 
 
    
 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<button type="button" class="day-btns" id="button1">Monday</button> 
 
<button type="button" class="day-btns" id="button2">Tuesday</button> 
 

 
<table id="myTable"> 
 
<thead> 
 
    <tr> 
 
    <th>Table</th> 
 
    </tr> 
 
</thead> 
 

 
<tbody> 
 
    <tr>   
 
    <td class="dates">13/02/2017 12:45 pm</td>  
 
    </tr> 
 

 
    <tr> 
 
    <td class="dates">14/02/2017 12:45 pm</td>  
 
    </tr> 
 

 
</tbody>

0

를 사용하여 요일에 따라 번호를 얻을 수있는 .weekday 방법, 당신은 등등 일요일, 월요일 1, 대한

가져옵니다 0을 얻거나의 일을 설정합니다 로케일에 따라 주.

예 :. 방법은 문서에서,

로케일이 주일의 첫날 월요일을 할당하는 경우

, 순간을 알고 지역이다

moment().weekday(); // code ran on a sunday 
// 0 
moment(new Date(1, 12, 2005)).weekday(); 
// 5 

주() 평일 (0)은 월요일이됩니다. 일요일이 주 첫날이면 moment(). weekday (0)가 일요일이됩니다.

+0

감사합니다.이 솔루션에 대해 알고 싶습니다. –