我正在使用Moment.js生成本周的日期数组。
然后我从系统获得一个日期数组,现在我想检查这些日期是否与当前创建的Moment.js相匹配。
如果是,那么我想返回它们的值(0,1,2 ..所以我可以相应地操纵dom对象。(例如:如果匹配返回当前周的周日和周二,那么我会突出显示它们)
如果日期属于当前一周,我只得到一个硬编码值来返回true或false。
var REFERENCE = moment();
var startOfWeek = REFERENCE.clone().startOf('week');
var endOfWeek = REFERENCE.clone().endOf('week');
var highlightDays = ['2017-08-23','2017-08-25','2017-08-29']; //highlight respective days ONLY they fall under current week
var days = [];
var day = startOfWeek;
while (day <= endOfWeek) {
days.push(day.toDate());
day = day.clone().add(1, 'd');
}
function isWithinThisWeek(momentDate) {
return momentDate.isBetween(startOfWeek, endOfWeek);
}
console.log("is it within this week? "+isWithinThisWeek(moment("2017-08-25")));
答案 0 :(得分:1)
编辑:修正了星期日的突出显示错误。您必须在subtract
来电中startOfWeek
一天isBetween()
{/ 1}}。
我认为这就是你想要的:
var REFERENCE = moment();
var startOfWeek = REFERENCE.clone().startOf('week');
var endOfWeek = REFERENCE.clone().endOf('week');
var highlightDays = ['2017-08-20','2017-08-21','2017-08-22','2017-08-23','2017-08-24','2017-08-25','2017-08-26'];
var days = [];
var day = startOfWeek;
while (day <= endOfWeek) {
days.push(day.toDate());
day = day.clone().add(1, 'd');
}
function isWithinThisWeek(momentDate) {
return momentDate.isBetween(startOfWeek.subtract(1, 'd'), endOfWeek.add(1, 'd'));
}
for(var i = 0; i < highlightDays.length; i++){
console.log(isWithinThisWeek(moment(highlightDays[i])));
if(isWithinThisWeek(moment(highlightDays[i]))){
var date_converted = new Date(highlightDays[i]);
$(".table td:eq("+date_converted.getDay()+")").addClass("highlight");
}
}
&#13;
.highlight{
background:yellow;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
<tbody>
<tr>
<td data-day="sunday">Sunday</td>
</tr>
<tr>
<td data-day="monday">Monday</td>
</tr>
<tr>
<td data-day="tuesday">Tuesday</td>
</tr>
<tr>
<td data-day="wednesday">Wednesday</td>
</tr>
<tr>
<td data-day="thursday">Thursday</td>
</tr>
<tr>
<td data-day="friday">Friday</td>
</tr>
<tr>
<td data-day="saturday">Saturday</td>
</tr>
</tbody>
</table>
&#13;
我在最后添加了一个for()
循环,并使用您的函数检查日期是否在当前周。使用此方法,我使用addClass()
突出显示匹配的记录。
答案 1 :(得分:1)
您可以使用jQuery Attribute Equals Selector [name="value"]和时刻format()
来突出显示highlightDays
数组当前周的日期。
在format()
中使用dddd
令牌,您将获得Sunday Monday ... Friday Saturday
,这是data-day
属性的值。
编辑:问题是isWithinThisWeek
方法在星期日给出了错误的结果,因为docs表示:
检查片刻是否在两个其他时刻之间,可选择查看单位比例(分钟,小时,天等)。 该比赛是独家的。
然后你可以使用两者:
isBetween(moment-like, moment-like, String, String)
传递[]
作为第四个论点,使其具有包容性。isSame(Moment|String|Number|Date|Array, String)
作为粒度参数传递一周这是一个实例:
var REFERENCE = moment();
var startOfWeek = REFERENCE.clone().startOf('week');
var endOfWeek = REFERENCE.clone().endOf('week');
var highlightDays = ['2017-08-18','2017-08-19','2017-08-20','2017-08-21','2017-08-22','2017-08-23','2017-08-24','2017-08-25','2017-08-26']; //highlight respective days ONLY they fall under current week
var days = [];
var day = startOfWeek;
while ( day.isBefore(endOfWeek) ) {
days.push(day.toDate());
day = day.clone().add(1, 'd');
}
function isWithinThisWeek(momentDate) {
return momentDate.isBetween(startOfWeek, endOfWeek, null, '[]');
// return momentDate.isSame(REFERENCE, 'week')
}
for(var i=0; i<highlightDays.length; i++){
var m = moment(highlightDays[i]);
if( isWithinThisWeek(m) ){
var dayName = m.format('dddd').toLowerCase();
$("[data-day='"+dayName+"']").addClass("highlight");
}
}
&#13;
.highlight{
background:yellow;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<table class="table">
<tbody>
<tr>
<td data-day="sunday">Sunday</td>
</tr>
<tr>
<td data-day="monday">Monday</td>
</tr>
<tr>
<td data-day="tuesday">Tuesday</td>
</tr>
<tr>
<td data-day="wednesday">Wednesday</td>
</tr>
<tr>
<td data-day="thursday">Thursday</td>
</tr>
<tr>
<td data-day="friday">Friday</td>
</tr>
<tr>
<td data-day="saturday">Saturday</td>
</tr>
</tbody>
</table>
&#13;
我的回答类似于已发布的两个,但是:
numToDays
临时数组答案 2 :(得分:0)
以下是您的追求。我添加了一个foreach循环和一个简单的天数,将int转换为日期名称。然后它根据数据日选择td
并突出显示它。
var REFERENCE = moment();
var startOfWeek = REFERENCE.clone().startOf('week');
var endOfWeek = REFERENCE.clone().endOf('week');
var highlightDays = ['2017-08-23','2017-08-25','2017-08-29']; //highlight respective days ONLY they fall under current week
var days = [];
var day = startOfWeek;
while (day <= endOfWeek) {
days.push(day.toDate());
day = day.clone().add(1, 'd');
}
function isWithinThisWeek(momentDate) {
return momentDate.isBetween(startOfWeek, endOfWeek);
}
var numToDays = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
highlightDays.forEach(function(element) {
if(isWithinThisWeek(moment(element))){
var date = moment(element);
var day = date.format("d");
$("td[data-day='"+numToDays[day]+"']").addClass("highlight");
}
});
&#13;
.highlight{
background:yellow;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data-2012-2022.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
<tbody>
<tr>
<td data-day="sunday">Sunday</td>
</tr>
<tr>
<td data-day="monday">Monday</td>
</tr>
<tr>
<td data-day="tuesday">Tuesday</td>
</tr>
<tr>
<td data-day="wednesday">Wednesday</td>
</tr>
<tr>
<td data-day="thursday">Thursday</td>
</tr>
<tr>
<td data-day="friday">Friday</td>
</tr>
<tr>
<td data-day="saturday">Saturday</td>
</tr>
</tbody>
</table>
&#13;
答案 3 :(得分:0)
我对Moment.js没有信心,但我会试着用普通的JS。 也许这有帮助。
var highlightDays = ['2017-08-23','2017-08-25','2017-08-29']; //highlight respective days ONLY they fall under current week
var date= new Date(); //Today Date
var today = date.getDay(); //Today Day of the week Number
var dayMSec = 1000 * 60 * 60 * 24; //a day in ms
var weekMSec = dayMSec * 7; // a week in ms
//Here i calculate the start of the week timestamp(Using dates the starting day will be Sunday). TS in in ms
var startWeekTS = date.getTime() - (dayMSec * today);
highlightDays.forEach(function(val, index){
if(isWithinThisWeek(val)){
//just logging
console.log(highlightDays[index] + ' is within this week');
}
});
function isWithinThisWeek(date) {
//generate a Date() from passed arg
date = new Date(date);
//getting the ts of the date
var dateMSec = date.getTime();
//subtracting start of the week ts and arg ts
var tsDiff = dateMSec - startWeekTS;
//if ts is between 0 and a week expressed in ms
if( tsDiff >= 0 && tsDiff< weekMSec){
//here we go
return true;
}
//nope
return false;
}