如何从日期数组生成简洁的日程可用性文本分析结果?
日程调度系统日期分析方案咨询
背景与需求
我正在开发一款带日程调度系统的应用,现有一组日期数据示例:
[ {date: "2023-09-14T23:00:00.000Z", value: 2}, {date: "2023-09-17T23:00:00.000Z", value: 4}, {date: "2023-10-24T23:00:00.000Z", value: 1} ]
需要生成简洁的文本分析内容,避免向用户展示过多冗余信息(比如一次性列出当月29天)。期望生成的句式如下:
"Schedule a call with xxx. xxx has a clear schedule on {approximateDate}. They have some availability on {clearDates}"
参数示例:
approximateDate = "post-16th June" clearDates = "10th and 12";
附带示例函数:
doctorScheduleAnalysis(){ let approximateDate = "post-16th June"; let clearDates = "10th and 12"; let analysis = "Schedule a call with <span>Dr name</span>. <span>Dr name</span> has a clear schedule "+ approximateDate +". They have some availability on "+clearDates+". Please select a day and a time."; return analysis; }
现在需要确定获取approximateDate和clearDates两个参数的最优方案:是取最新日期并填充当月剩余可用天数,还是覆盖两周这类跨时间段的可用日期?同时需要处理更复杂的日期间隔场景(如10th-20th、29th-30th)。
最优方案建议
一、approximateDate 生成逻辑
优先采用**「近期时间段+关键节点」**组合策略,而非单纯取最新日期或固定两周范围:
- 若可用日期集中在未来7天内:直接用「本周内」「未来3天」这类模糊表述,匹配用户即时预约需求。
- 若可用日期分散且存在跨月/长期空档:取最早可用日期作为节点,生成「XX日之后」的表述(比如示例中的"post-16th June");若当月剩余日期多数可用,直接用「当月剩余时段」。
- 避免固定两周范围:如果可用日期不在两周内,这种表述会误导用户,反而降低体验。
二、clearDates 生成逻辑
核心是合并连续日期、精简零散日期,减少信息负载:
- 合并连续日期:把连续可用日转化为区间(比如10、11、12日→"10th-12th";29、30日→"29th-30th")。
- 精简零散日期:零散日期≤3个时直接列举(比如10、12、15日→"10th, 12th and 15th");超过3个时,保留前2个+「等X个日期」的概括(比如10、12、15、17日→"10th, 12th等4个日期")。
- 区分跨月日期:若可用日期跨月,需标注月份(比如"9月17日、10月24日"),避免歧义。
三、示例数据处理演示
以给定的示例日期数据为例:
- 可用日期:9月14日、9月17日、10月24日
approximateDate:取最早可用日期节点,生成「9月14日之后」clearDates:三个零散跨月日期,直接列举为"9月14日、9月17日和10月24日"
四、代码实现思路
新增两个辅助函数分别处理两个参数,整合到原函数中:
// 处理approximateDate function getApproximateDate(availableDates) { const sortedDates = availableDates.map(d => new Date(d.date)).sort((a,b) => a - b); const earliestDate = sortedDates[0]; const month = earliestDate.toLocaleString('zh-CN', {month: 'long'}); const day = earliestDate.getDate(); // 检查未来7天内是否有可用日期 const sevenDaysLater = new Date(); sevenDaysLater.setDate(sevenDaysLater.getDate() + 7); if (earliestDate <= sevenDaysLater) { return "本周内"; } return `${month}${day}日之后`; } // 处理clearDates function getClearDates(availableDates) { const sortedDates = availableDates.map(d => { const date = new Date(d.date); return { month: date.toLocaleString('zh-CN', {month: 'long'}), day: date.getDate() }; }).sort((a,b) => { // 按月份、日期排序 if (a.month !== b.month) return a.month.localeCompare(b.month); return a.day - b.day; }); // 合并连续日期 const merged = []; let currentStart = sortedDates[0]; for (let i = 1; i < sortedDates.length; i++) { const prev = sortedDates[i-1]; const curr = sortedDates[i]; // 同月份且日期连续 if (prev.month === curr.month && curr.day === prev.day + 1) { continue; } else { if (currentStart.day === prev.day) { merged.push(`${currentStart.month}${currentStart.day}日`); } else { merged.push(`${currentStart.month}${currentStart.day}日-${prev.month}${prev.day}日`); } currentStart = curr; } } // 处理最后一组 const last = sortedDates[sortedDates.length - 1]; if (currentStart.day === last.day) { merged.push(`${currentStart.month}${currentStart.day}日`); } else { merged.push(`${currentStart.month}${currentStart.day}日-${last.month}${last.day}日`); } // 精简输出 if (merged.length <= 3) { return merged.join('、').replace(/、([^、]+)$/, '和$1'); } else { return `${merged[0]}、${merged[1]}等${merged.length}个时段`; } } // 整合到原函数 function doctorScheduleAnalysis(doctorName, availableDates) { const approximateDate = getApproximateDate(availableDates); const clearDates = getClearDates(availableDates); return `Schedule a call with <span>${doctorName}</span>. <span>${doctorName}</span> has a clear schedule ${approximateDate}. They have some availability on ${clearDates}. Please select a day and a time.`; }
内容的提问来源于stack exchange,提问作者The Old County
相关产品推荐
相关产品推荐

