[英]What's the most efficient way to combine dates?
Records in my database look like this: 我数据库中的记录如下所示:
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
And inside my application I have to combine them into 1 record looking like this: 在我的应用程序内部,我必须将它们合并为1条记录,如下所示:
From: 2012-01-16 06:20:00 To: 2012-01-16 06:50:00
Sometimes records are not in any order. 有时记录不是按任何顺序排列的。 They might look like that:
它们可能看起来像这样:
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
如果这是一个时间戳,则可以使用MIN(from)和MAX(to)。
Probably not the most efficient way, but I would do it like this: 可能不是最有效的方法,但我会这样做:
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.