我的数据库中的记录如下所示:
From: 2012-01-16 06:20:00 To: 2012-01-16 06:30:00
From: 2012-01-16 06:30:00 To: 2012-01-16 06:40:00
From: 2012-01-16 06:40:00 To: 2012-01-16 06:50:00
在我的应用程序中,我必须将它们组合成1个记录,如下所示:
From: 2012-01-16 06:20:00 To: 2012-01-16 06:50:00
有时记录不是任何顺序。他们可能看起来像这样:
From: 2012-01-16 06:20:00 To: 2012-01-16 06:30:00
From: 2012-01-16 06:40:00 To: 2012-01-16 06:50:00
From: 2012-01-16 06:30:00 To: 2012-01-16 06:40:00
答案 0 :(得分:0)
如果这是时间戳,您可以使用MIN(从)和MAX(到)。
答案 1 :(得分:0)
可能不是最有效的方式,但我会这样做:
let timeRanges = [];
timeRanges.push({
from: new Date(Date.parse('2012-01-16 06:20:00')),
to: new Date(Date.parse('2012-01-16 06:30:00'))
});
timeRanges.push({
from: new Date(Date.parse('2012-01-16 06:40:00')),
to: new Date(Date.parse('2012-01-16 06:50:00'))
});
timeRanges.push({
from: new Date(Date.parse('2012-01-16 06:30:00')),
to: new Date(Date.parse('2012-01-16 06:40:00'))
});
let combinedTimeRange = {
from: null,
to: null
};
for(let timeRange of timeRanges) {
if(combinedTimeRange.from === null
|| combinedTimeRange.from.getTime() > timeRange.from.getTime()) {
combinedTimeRange.from = timeRange.from;
}
if(combinedTimeRange.to === null
|| combinedTimeRange.to.getTime() < timeRange.to.getTime()) {
combinedTimeRange.to = timeRange.to;
}
}
// just a test ..
console.log(combinedTimeRange.from.getMinutes());
console.log(combinedTimeRange.to.getMinutes());