【问题标题】:JavaScript filter data by week day and timestampsJavaScript 按工作日和时间戳过滤数据
【发布时间】:2022-01-07 11:21:07
【问题描述】:

在我的个人项目中,用户可以定义他们不想被打扰的时间。这些时间包括您选择的星期几,以及间隔的开始和结束时间戳。

基本上,在一周中的那些日子以及用户规定的这些时间间隔内,我无法创建任务。也就是说,“最终”数组应该只包含可用于创建任务的用户。

我目前的代码如下:

const data = [
    {
        firstName: 'John',
        lastName: 'Doe',
        schedules: [
            {
                days: ['Tue', 'Wed', 'Fri'],
                start: '2022-01-07T10:50:00.859Z',
                end: '2022-01-07T11:30:00.859Z',
            },
            {
                days: ['Mon'],
                start: '2022-01-07T13:30:00.859Z',
                end: '2022-01-07T14:30:00.859Z',
            },
        ],
    },
    {
        firstName: 'Aaron',
        lastName: 'Park',
        schedules: [
            {
                days: ['Wed', 'Fri'],
                start: '2022-01-07T13:30:00.859Z',
                end: '2022-01-07T14:30:00.859Z',
            },
            {
                days: ['Mon'],
                start: '2022-01-07T13:30:00.859Z',
                end: '2022-01-07T14:30:00.859Z',
            },
        ],
    },
];

const todayWeekDay = new Date().toLocaleString('en-us', { weekday: 'short' });
const currentTime = new Date();

const filteredData = data.filter((user) => {
    const filteredSchedules = user.schedules.filter((schedule) => {
        const days = schedule.days;
        if (days.includes(todayWeekDay)) {
            const startTime = new Date(schedule.start);
            const endTime = new Date(schedule.end);
            if (currentTime >= startTime && currentTime <= endTime) {
                return false;
            }
            return true;
        }
        return false;
    });
    if (filteredSchedules.length) {
        return true;
    }
    return false;
});

console.log(filteredData)

我还没有测试过很多边缘情况,但我注意到有时当我“玩”一周中的几天时,有时用户应该出现在“最终”数组中,但事实并非如此。

在最终结果中,必须在以下情况下返回用户:

  • 如果用户没有日程安排
  • 如果当前工作日不在 days 数组中
  • 如果当前工作日在 days 数组中,如果当前时间不在两个时间戳之间,则必须返回用户(在这两个时间戳中我只关心小时和分钟)

我期望的输出可能会根据一周中的当前日期和时间而变化。但例如,如果今天是 2022 年 1 月 21 日,对应于星期五。在上午 10:50 到 11:30 之间,结果应如下所示:

[{
  "firstName": "Aaron",
  "lastName": "Park",
  "schedules": [
    {
      "days": [
        "Wed",
        "Fri"
      ],
      "start": "2022-01-07T13:30:00.859Z",
      "end": "2022-01-07T14:30:00.859Z"
    },
    {
      "days": [
        "Mon"
      ],
      "start": "2022-01-10T13:30:00.859Z",
      "end": "2022-01-12T14:30:00.859Z"
    }
  ]
}] 

在这些时间之外,结果应该是这样的:

[{
  "firstName": "John",
  "lastName": "Doe",
  "schedules": [
    {
      "days": [
        "Tue",
        "Wed",
        "Fri"
      ],
      "start": "2022-01-07T10:50:00.859Z",
      "end": "2022-01-07T11:30:00.859Z"
    },
    {
      "days": [
        "Mon"
      ],
      "start": "2022-01-10T13:30:00.859Z",
      "end": "2022-01-12T14:30:00.859Z"
    }
  ]
}, {
  "firstName": "Aaron",
  "lastName": "Park",
  "schedules": [
    {
      "days": [
        "Wed",
        "Fri"
      ],
      "start": "2022-01-07T13:30:00.859Z",
      "end": "2022-01-07T14:30:00.859Z"
    },
    {
      "days": [
        "Mon"
      ],
      "start": "2022-01-10T13:30:00.859Z",
      "end": "2022-01-12T14:30:00.859Z"
    }
  ]
}] 

【问题讨论】:

  • 请提供预期的输出。
  • 我将编辑问题并添加它。
  • 您似乎也在 if 条件下比较这一天。 if (currentTime &gt;= startTime &amp;&amp; currentTime &lt;= endTime) - 在这里您应该只比较时间部分并忽略日期。 startTime, endTime 包括日期和时间
  • 如果一周中的当前日期包含在 days 数组中并且当前时间不在时间间隔内,您的代码将在最终结果中返回一个用户。不清楚这是否是你的目标
  • 对不起,如果我不能很清楚,我用一些我想在最终结果中考虑的主题更新了问题。

标签: javascript node.js typescript


【解决方案1】:

您可以使用这样的方法忽略日期并仅比较时间。

function fallsInInterval(start, end, date) {
    const now = new Date();
    const s = new Date(now.getFullYear(), now.getMonth(), now.getDate(), start.getHours(), start.getMinutes(), start.getSeconds());
    const e = new Date(now.getFullYear(), now.getMonth(), now.getDate(), end.getHours(), end.getMinutes(), end.getSeconds());
    const d = new Date(now.getFullYear(), now.getMonth(), now.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());
    return d >= s && d <= e;
}

上述方法所做的只是创建新的日期对象,其中所有的日期组件都相同,但保留了原始时间,这使得比较更容易。

所以,而不是

if (currentTime >= startTime && currentTime <= endTime) {
    return false;
}

你会使用

if (fallsInInterval(startTime, endTime, currentTime)) {
    return false;
}

【讨论】:

    猜你喜欢
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多