简体   繁体   中英

Moment-Range: Need to divide day into 15 minute slices

At present I can only divide the day into 1 hour blocks. But I need the ranges in 15 minute steps.

Moment-Range Documentation

This is my present code:

function iterateOverDayByIntervalOfHours(inputJSON){
    var day = getDayFromFromJSON(inputJSON);

    var start = new Date("2016-05-04T00:00:00.000Z");
    var end   = new Date("2016-05-04T23:59:59.999Z");

    var range = moment.range(start, end);
    var slices = {}
    range.by( 'hours', function(moment) {    
        console.log(moment);
        slices["moment"] = moment
        console.log("slices: "+ slices["moment"]);
        var ROTsAccumulatedForInterval =  getAccumulatedROTForTimeIntervall(range);
        var NumberOfFlightsForInterval = getNumberOfFlightsForTimeIntervall(range);
    });
    console.log(slices["moment"]);
}

any ideas?

Here is another way using moment lib with moment-range extension:

const day_start = moment().startOf('day').hours(7); // 7 am
const day_end   = moment().startOf('day').hours(22) // 10 pm
const day = moment.range(day_start, day_end)
const time_slots = Array.from(day.by('minutes', {step: 30}))

in

Array.from(day.by('minutes', {step: 30}))

You can change 'minutes' for hours, days, weeks and step for how many minutes/hours/days you want to chunk by.

return value will be

[ moment("2017-10-20T07:00:00.000"),
  moment("2017-10-20T07:30:00.000"),
  moment("2017-10-20T08:00:00.000"),
  moment("2017-10-20T08:30:00.000"),
  moment("2017-10-20T09:00:00.000"),
  moment("2017-10-20T09:30:00.000"),
  ...
  moment("2017-10-20T19:30:00.000"),
  moment("2017-10-20T20:00:00.000"),
  moment("2017-10-20T20:30:00.000"),
  moment("2017-10-20T21:00:00.000"),
  moment("2017-10-20T21:30:00.000"),
  moment("2017-10-20T22:00:00.000") ]

This doesn't use moment and it's not implemented in your function yet, but this is how I would try to get an object of 15min-chunks. I hope, this is what you are looking for.

 var start = new Date("2016-05-04T00:00:00.000Z"); var end = new Date("2016-05-04T23:59:59.999Z"); var slices = {}; var count = 0; var moment; while (end >= start) { start = new Date(start.getTime() + (15 * 60 * 1000)); slices[count] = start; count++; } console.log(slices);

Too late but might be helpful, I'm doing the following to divide a day into hour date ranges.

using lodash and moment

const generateDayHours = (x = 24) => {
  const hoursArr = [];
  _.times(x, (i) => {
    hoursArr.push({
      fromDate: moment().startOf('day').add(x - (i + 1), 'hour').startOf('hour'),
      toDate: moment().startOf('day').add(x - (i + 1), 'hour').endOf('hour')
    });
  });
  return hoursArr;
};

jsbin to test

You can also use something like this.

// Take a starting point
const start = moment('00:00:00', 'HH:mm:ss');

// Take a end point
const end = moment('23:59:59', 'HH:mm:ss');
const timeSeries = [];

while (start.isSameOrBefore(end)) {
  // add 15 minutes to the starting point
  timeSeries.push(start.add(15, 'm').format('HH:mm'));
}

console.log(timeSeries);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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