最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - divide date range into known number of equal chunks - Stack Overflow

programmeradmin1浏览0评论

I've seen Split date range into date range chunks and Split date range into several specific date range chunks, that is not what I'm looking for.

I'm looking for simple function from momenjs, that will help me with known start date, end date and number of chunks (slices) to return an array of equal chunks.

07-15-20 start date 07-27-20 end date I want to split it into 12 chunks

result = [07.15.20 00:00, 07.16.20 00:00, 07.17.20 00:00...]

Trouble starts when date is not so straightforward.

For ex. if I have start date as

    minTimeX = moment(sortedTests[0].date).format('x');
    maxTimeX = moment(sortedTests.length - 1].date).format('x');

and I want to divide this into equal 12 chunks and I cannot figure how I can calculate it manually. Is it implemented in momentjs?

I've seen Split date range into date range chunks and Split date range into several specific date range chunks, that is not what I'm looking for.

I'm looking for simple function from momenjs, that will help me with known start date, end date and number of chunks (slices) to return an array of equal chunks.

07-15-20 start date 07-27-20 end date I want to split it into 12 chunks

result = [07.15.20 00:00, 07.16.20 00:00, 07.17.20 00:00...]

Trouble starts when date is not so straightforward.

For ex. if I have start date as

    minTimeX = moment(sortedTests[0].date).format('x');
    maxTimeX = moment(sortedTests.length - 1].date).format('x');

and I want to divide this into equal 12 chunks and I cannot figure how I can calculate it manually. Is it implemented in momentjs?

Share Improve this question edited Aug 6, 2020 at 6:35 deathfry asked Aug 5, 2020 at 21:05 deathfrydeathfry 7441 gold badge12 silver badges31 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

not sure if moment supports what you're looking for, but you can do it in javascript like this

function splitDateIntoEqualIntervals(startDate, endData, numberOfIntervals){

 let diff =  endData.getTime() - startDate.getTime();
 let intervalLength = diff/numberOfIntervals;
 let intervals = [];
 for(let i = 1 ; i <= numberOfIntervals;i++)
   intervals.push(new Date(startDate.getTime()+i*intervalLength))
 return intervals;

}

If you need chunks you probably need start, end and maybe avg of these chunks. So you can use this function similar to these proposed by @ibrachim

const splitDateIntoEqualIntervals = (startDate: Date, endDate: Date, numberOfIntervals: number):{start: Date, end: Date, avg: Date}[] => {
  const intervalLength = (endDate.getTime() - startDate.getTime()) / numberOfIntervals
  return [...(new Array(numberOfIntervals))]
    .map((e, i) => {
      return {
        start: new Date(startDate.getTime() + i * intervalLength),
        avg: new Date(startDate.getTime() + (i + 0.5) * intervalLength),
        end: new Date(startDate.getTime() + (i + 1) * intervalLength)
      }
    })
}

It returns array with chunks without arbitral of choice of start or end.

发布评论

评论列表(0)

  1. 暂无评论