我有一个看起来像这样的对象:
const availability = {
week1: {
mon: false,
tue: true,
wed: true,
thu: true,
fri: false,
},
week2: {
mon: false,
tue: true,
wed: true,
thu: true,
fri: false,
},
week3: {
mon: false,
tue: true,
wed: true,
thu: true,
fri: false,
},
week4: {
mon: false,
tue: true,
wed: true,
thu: true,
fri: false,
},
};
这显示了当月第一天,第二天,...某周某人的空缺情况。
我现在需要的是每天的真实日期。因此,例如,本月第一周的星期二日期,本月第二周的星期二日期,等等。
我已经检查了date-fns和moment.js,但是我不知道如何解决这个问题。
答案 0 :(得分:1)
如果我们假设要求是从一个月的第一个星期一开始,那么您可以将该月分为几周,并使用availability
进行映射。我在这里使用官方的moment-range插件,但是您可以通过其他方式计算这些范围:
const Moment = require('moment');
const MomentRange = require('moment-range');
const moment = MomentRange.extendMoment(Moment);
const availability = { week1: { mon: false, tue: true, wed: true, thu: true, fri: false, }, week2: { mon: false, tue: true, wed: true, thu: true, fri: false, }, week3: { mon: false, tue: true, wed: true, thu: true, fri: false, }, week4: { mon: false, tue: true, wed: true, thu: true, fri: false, }, };
const calcDays = (data, days) => {
// get the assumed first Monday of the current month
let fM = moment().startOf('month').isoWeekday('Monday')
let cM = moment().month()
// get the actual first Monday
fM = fM.month() < cM ? fM.add(1, 'week').isoWeekday('Monday') : fM.month() > cM ? fM.subtract(1, 'week').isoWeekday('Monday') : fM
// Calc the range based on first Monday and end of month
const range = moment.range(fM, moment().endOf('month'));
// Break into weeks
const weeks = [...Array.from(range.by('week')), moment().endOf('month')]
return Object.values(availability).reduce((r,c,i) => {
var weekRanges = Array.from(moment.range(weeks[i], weeks[i + 1]).by('day'))
Object.entries(c).map(([k, v], j) => v ? r.push(weekRanges[j].format()) : v)
return r
}, [])
}
console.log(calcDays(availability))
输出(于2018年11月8日):
[ '2018-11-06T00:00:00+00:00',
'2018-11-07T00:00:00+00:00',
'2018-11-08T00:00:00+00:00',
'2018-11-13T00:00:00+00:00',
'2018-11-14T00:00:00+00:00',
'2018-11-15T00:00:00+00:00',
'2018-11-20T00:00:00+00:00',
'2018-11-21T00:00:00+00:00',
'2018-11-22T00:00:00+00:00',
'2018-11-27T00:00:00+00:00',
'2018-11-28T00:00:00+00:00',
'2018-11-29T00:00:00+00:00' ]
您可以看到它working here
答案 1 :(得分:1)
尝试
function isAGoodDay (date) {
var weekOfMonth = Math.floor(date.getDate() / 7) + 1;
var dayOfWeek = date.getDay();
var day = days[dayOfWeek];
if (day === '') return false;
return availability['week'+weekOfMonth][day];
}
function listGoodDays(year, month) {
var lastDay = new Date(new Date(year, month, 1)-1).getDate();
return listOfdays[lastDay]
.filter((d) => (isAGoodDay(new Date(year, month-1, d))))
.map((d) => (new Date(year, month-1, d)));
}
const availability = {
week1: {
mon: false,
tue: true,
wed: true,
thu: true,
fri: false,
},
week2: {
mon: false,
tue: true,
wed: true,
thu: true,
fri: false,
},
week3: {
mon: false,
tue: true,
wed: true,
thu: true,
fri: false,
},
week4: {
mon: false,
tue: true,
wed: true,
thu: true,
fri: false,
},
week5: {
mon: false,
tue: true,
wed: true,
thu: true,
fri: false,
}
};
var days = ['','mon','tue','wed','thu','fri',''];
var listOfdays =
{"28" : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]
,"29" : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
,"30" : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
,"31" : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]
};
console.log( listGoodDays(2018, 11) );