[英]Moment JS date range
我想根据星期几显示/隐藏表格行。 我目前使用的代码根据24小时时间段而非日历天来显示/隐藏:
<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();
}
});
});
由于moment.format()中的“ dddd”代表星期几,因此您可以轻松地将其与按钮标签进行比较。
通过向它们添加通用类,将单击处理程序附加到所有按钮。 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.