You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS按format数组分组日期时拆分非连续日期区间的实现方法

解决方法

你需要将原有的map方法替换为flatMap来适配单个分组拆分为多个区间的1:N映射场景,再新增连续日期校验逻辑拆分非连续区间即可,不需要嵌套map处理。

核心逻辑说明

  1. 新增日期连续判断能力:将ISO格式日期转为对应自然日零点的时间戳,判断相邻两个日期的时间差是否≤1天(兼容同一天不同时分秒的场景)
  2. 对每个format对应长度的日期分组,遍历拆分所有连续区间
  3. 用flatMap代替原有map,自动将每个分组返回的多个区间对象扁平化到最终结果数组

完整实现代码

let format = [3, 3, 1, 5, 4, 4, 3, 5, 13, 10, 3, 5, 5, 2, 2, 10];
let dates = [
  "2021-10-04T04:00:00.000Z",
  "2021-10-06T04:00:00.000Z",
  "2021-10-07T04:00:00.000Z",
  "2021-10-13T04:00:00.000Z",
  "2021-10-14T04:00:00.000Z",
  "2021-10-15T04:00:00.000Z",
  "2021-10-15T04:00:00.000Z",
  "2021-10-17T22:00:00.000Z",
  "2021-10-18T22:00:00.000Z",
  "2021-10-19T22:00:00.000Z",
  "2021-10-20T22:00:00.000Z",
  "2021-10-21T22:00:00.000Z",
  "2021-10-17T22:00:00.000Z",
  "2021-10-18T22:00:00.000Z",
  "2021-10-19T22:00:00.000Z",
  "2021-10-20T22:00:00.000Z",
  "2021-10-19T04:00:00.000Z",
  "2021-10-20T04:00:00.000Z",
  "2021-10-21T04:00:00.000Z",
  "2021-10-22T04:00:00.000Z",
  "2021-10-19T04:00:00.000Z",
  "2021-10-20T04:00:00.000Z",
  "2021-10-21T04:00:00.000Z",
  "2021-10-25T04:00:00.000Z",
  "2021-10-26T04:00:00.000Z",
  "2021-10-27T04:00:00.000Z",
  "2021-10-28T04:00:00.000Z",
  "2021-10-29T04:00:00.000Z",
  "2021-10-25T04:00:00.000Z",
  "2021-10-26T04:00:00.000Z",
  "2021-10-27T04:00:00.000Z",
  "2021-10-28T04:00:00.000Z",
  "2021-10-29T04:00:00.000Z",
  "2021-11-01T04:00:00.000Z",
  "2021-11-02T04:00:00.000Z",
  "2021-11-03T04:00:00.000Z",
  "2021-11-04T04:00:00.000Z",
  "2021-11-05T04:00:00.000Z",
  "2021-11-08T04:00:00.000Z",
  "2021-11-09T04:00:00.000Z",
  "2021-11-10T04:00:00.000Z",
  "2021-11-01T04:00:00.000Z",
  "2021-11-02T04:00:00.000Z",
  "2021-11-03T04:00:00.000Z",
  "2021-11-04T04:00:00.000Z",
  "2021-11-05T04:00:00.000Z",
  "2021-11-08T04:00:00.000Z",
  "2021-11-09T04:00:00.000Z",
  "2021-11-10T04:00:00.000Z",
  "2021-11-11T04:00:00.000Z",
  "2021-11-12T04:00:00.000Z",
  "2021-11-11T04:00:00.000Z",
  "2021-11-12T04:00:00.000Z",
  "2021-11-13T04:00:00.000Z",
  "2021-11-15T04:00:00.000Z",
  "2021-11-16T04:00:00.000Z",
  "2021-11-17T04:00:00.000Z",
  "2021-11-18T04:00:00.000Z",
  "2021-11-19T04:00:00.000Z",
  "2021-11-16T04:00:00.000Z",
  "2021-11-17T04:00:00.000Z",
  "2021-11-18T04:00:00.000Z",
  "2021-11-19T04:00:00.000Z",
  "2021-11-20T04:00:00.000Z",
  "2021-11-23T04:00:00.000Z",
  "2021-11-24T04:00:00.000Z",
  "2021-11-23T04:00:00.000Z",
  "2021-11-24T04:00:00.000Z",
  "2022-01-05T04:00:00.000Z",
  "2022-01-06T04:00:00.000Z",
  "2022-01-07T04:00:00.000Z",
  "2022-01-10T04:00:00.000Z",
  "2022-01-11T04:00:00.000Z",
  "2022-01-12T04:00:00.000Z",
  "2022-01-13T04:00:00.000Z",
  "2022-01-14T04:00:00.000Z",
  "2022-01-17T04:00:00.000Z",
  "2022-01-18T04:00:00.000Z"
];

// 辅助函数:将ISO日期转为当日零点时间戳,消除时分秒的影响
const getDayTimestamp = (isoStr) => {
  const date = new Date(isoStr);
  return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
}
const ONE_DAY = 86400 * 1000; // 一天的毫秒数

const res = format.flatMap(num => {
  const arr = dates.splice(0, num);
  if (!arr.length) return [];

  const intervals = [];
  let currentStart = arr[0];
  let currentEnd = arr[0];

  for (let i = 1; i < arr.length; i++) {
    const prevDayTs = getDayTimestamp(currentEnd);
    const currDayTs = getDayTimestamp(arr[i]);
    // 相邻日期差≤1天则属于同一连续区间(包含同一天的情况)
    if (currDayTs - prevDayTs <= ONE_DAY) {
      currentEnd = arr[i];
    } else {
      // 不连续则保存当前区间,开启新的区间
      intervals.push({ start: currentStart, end: currentEnd });
      currentStart = arr[i];
      currentEnd = arr[i];
    }
  }
  // 加入最后一个区间
  intervals.push({ start: currentStart, end: currentEnd });
  return intervals;
})

console.log(res);

效果说明

运行后你提到的最后一个长度为10的分组会自动拆分为你要求的3个连续区间对象,所有存在非连续日期的分组都会按实际连续情况拆分。

内容的提问来源于stack exchange,提问作者davs0ft

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 02:27:02