简体   繁体   English

Moment-Range:需要将一天分成 15 分钟的片段

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

At present I can only divide the day into 1 hour blocks.目前我只能将一天划分为 1 小时块。 But I need the ranges in 15 minute steps.但我需要 15 分钟的范围。

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:这是使用带有moment-range扩展的moment库的另一种方法:

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.您可以将'minutes'更改为hours, days, weeksstep更改您想要分块的minutes/hours/days

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.这不使用 moment 并且尚未在您的函数中实现,但这就是我尝试获取 15 分钟块对象的方式。 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使用 lodash 和 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 jsbin进行测试

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);

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

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