简体   繁体   English

合并日期最有效的方法是什么?

[英]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.

相关问题 在日期范围内查找丢失日期的最有效方法是什么 - What is the most efficient way of finding missing dates within a daterange 将数据存储到Firebase中的多个引用的最有效方法是什么? - What's the most efficient way to store data to multiple refs in firebase? 使用 Angular 组件/指令绑定的最有效方法是什么? - What's the most efficient way to use Angular component/directive bindings? 使用JavaScript评估字符串是否是回文符的最有效方法是什么? - What's the most efficient way to evaluate if a string is a palindrome using Javascript? 为网页的动画标头编码的最有效方法是什么? - What's the most efficient way of coding an animated header for a webpage? 在Java中通过字符串的最有效方式是什么? - What is the most efficient way to go through string's chars in Javascript? 在JavaScript中,将标签转换为链接的最有效方法是什么? - In javascript, what's the most efficient way to I turn tags into links? 隐藏移动版网站内容的最有效方法是什么? - What's the most efficient way to hide content for a mobile version of a site? 什么是在IE8 +中追加元素的最有效方法? - what's the most efficient way to append elements in IE8+? 比较 javascript 中的 2 个元素的最有效方法是什么? - What's the most efficient way to compare 2 elements in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM