0

我有一个看起来像这样的对象:

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,
        },
    };

这显示某人在当月的第 1、2、...周有空。

我现在需要的是真实的每一天的日期。例如,本月第一周的星期二日期,本月第二周的星期二日期等等。

我已经检查了 date-fns 和 moment.js,但我不知道如何解决这个问题。

4

2 回答 2

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) );

于 2018-11-08T20:18:18.150 回答
1

如果我们假设要求是我们从每月的第一个星期一开始,那么您可以将月份分成几周并使用您availability的地图来映射它们。我在这里使用官方的矩范围插件,但您可以通过其他方式计算这些范围:

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' ]

你可以看到它在这里工作

于 2018-11-08T18:20:04.953 回答