2017-07-28 58 views
0

记录在我的数据库是这样的:什么是组合日期最有效的方法?

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

这个Timestam()?或选择的时间? –

+0

它是一个时间戳 – ChristoK

+0

然后订单必须排序 –

回答

0

如果这是一个时间戳,你可以使用MIN(from)和MAX(to)。

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()); 
相关问题