简体   繁体   English

从日期集合中查找日期范围

[英]Find date ranges from a collection of dates Javascript

I know this is similar to this Question , but I need different results and in JavaScript. 我知道这类似于这个Question ,但是我需要不同的结果以及JavaScript。

I have an string of dates like this, that may not be in order. 我有这样的日期字符串,可能顺序不正确。 03/27/2017,03/28/2017,03/29/2017,04/04/2017,04/05/2017,04/06/2017,04/12/2017,04/13/2017,04/14/2017, 05/02/2017 3月27日/ 2017,03 / 28 / 2017,03 / 29 / 2017,04 / 04 / 2017,04 / 05 / 2017,04 / 06 / 2017,04 / 12 / 2017,04 / 13 / 2017,04 / 14/2017,05/02/2017

Date format is mm/dd/yyyy 日期格式是mm / dd / yyyy

I need to split this into an array of dates, which I can do. 我需要将其拆分为日期数组,我可以这样做。 But then I need to loop through all dates and if the dates are connected then it needs to be a date range. 但是然后我需要遍历所有日期,如果日期已连接,则它必须是一个日期范围。 If there is a single date, then it would be a single date range. 如果有一个日期,那么它将是一个日期范围。

So I would need these results from the above dates. 因此,我需要以上日期的这些结果。

[
    {'start': 03/27/2017, 'end': 03/29/2017 }, 
    {'start': 04/04/2017, 'end': 04/06/2017}, 
    {'start': 04/12/2017, 'end': 04/14/2017 }, 
    {'start': 05/02/2017, 'end': 05/02/2017}
]

I am doing this app in JavaScript, Typescript, lodash, angular 1.6. 我正在用JavaScript,Typescript,lodash,angular 1.6编写此应用程序。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

EDIT 编辑

I have a popup calendar that the user can select multiple dates. 我有一个弹出日历,用户可以选择多个日期。 If the dates are consecutive then that is the date range, if it is a single date then that alone will be the date range. 如果日期是连续的,则为日期范围;如果为单个日期,则仅该日期为日期范围。 This is needed for the user to select their desired dates off, then these will be inserted into the DB. 用户需要选择这些日期,然后将这些日期插入数据库。

Here is how I did it . 这是我的做法 I converted them to dates, and then did a math calculation to see if the next date was bigger than a day or not. 我将它们转换为日期,然后进行数学计算以查看下一个日期是否大于一天。 If it was, then I stopped the current date range and started the next one. 如果是,那么我停止了当前日期范围并开始了下一个日期范围。

let string = '03/27/2017,03/28/2017,03/29/2017,04/04/2017,04/05/2017,04/06/2017,04/12/2017,04/13/2017,04/14/2017, 05/02/2017';
  //split them and pull out any white spaces from beginning and end
  let dates = string.split(',').map(s=>{
    s = s.trim();
    let nums = s.split('/');
    let d = new Date(nums[2], nums[0], nums[1]);
    return {date:d, string: s};
  });
  let currentStart = dates[0];
  let result = [];
  for(let i = 1; i < dates.length; i++){
    let {date, string} = dates[i];
    //If last date, add range
    if(i == dates.length -1){
      console.log("hello");
      result.push({start: currentStart.string ,end: string});
    } else {
      let prevDate = dates[i-1] || currentStart; //in case prevDate is undefined
      let nextDate = dates[i+1];
      let diff = nextDate.date.getTime() - date.getTime();
      if(diff > (24 * 60 * 60 * 1000)){
        result.push({start: currentStart.string ,end: string});
        currentStart = nextDate;
      }
    }
  }

  console.log(result, 'result');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM