admin管理员组

文章数量:1386703

I want to display array of durations given the startTime, endTime, and interval.

let value = {
  interval: '00:30:00',
  startTime: '03:00:00',
  endTime: '20:00:00'
};

Sample output must be an array of durations with the given interval

-startTime minus the interval, so the new startTime = 02:30:00

[0] => '02:30 am - 03:00 am'
[1] => '03:00 am - 03:30 am'
[2] => '03:30 am - 04:00 am'
[3] => '04:00 am - 04:30 am'
...... 
[n] => '07:30 pm - 08:00 pm'

Here's my function

function showTimeIntervals(value) {
  let result = value.interval.split(","); 
  let start = "";
  let timeNotation = '';
  let time = '';
  for(let i in result) {
    let hr = moment(result[i], 'HH:mm').format('HH');
    let min = moment(result[i], 'HH:mm').format('mm');
    hr = (hr != 0) ? parseInt(hr, 10) : '';
    min = (min != 0) ? parseInt(min, 10) : '';
    if(hr != 0) {
      time = hr;
      timeNotation = 'hour';
      start = moment(value.startTime, 'hh:mm a').subtract(hr, 'hour');
    } else {
      time = min;
      timeNotation = 'minutes';
      start = moment(value.startTime, 'hh:mm a').subtract(min, 'minutes');
    }
  }
  var end = moment(value.endTime, 'hh:mm a');
  if(end < start)
    end = end.add(1, 'd');
  var finalResult = [];
  var current = moment(start);
  while (current <= end) {
    finalResult.push(current.format('hh:mm a'));
    current.add(time, timeNotation);
  }
  return finalResult;
}

But it gives individual time not a duration

 [0] => '02:30 am'
 [1] => '03:00 am'
 [2] => '03:30 am'
 [3] => '04:00 am'
 ......
 [n] => '08:00 pm'

Here's a fiddle -> /

Is there an easiest way a moment js can do that?

I want to display array of durations given the startTime, endTime, and interval.

let value = {
  interval: '00:30:00',
  startTime: '03:00:00',
  endTime: '20:00:00'
};

Sample output must be an array of durations with the given interval

-startTime minus the interval, so the new startTime = 02:30:00

[0] => '02:30 am - 03:00 am'
[1] => '03:00 am - 03:30 am'
[2] => '03:30 am - 04:00 am'
[3] => '04:00 am - 04:30 am'
...... 
[n] => '07:30 pm - 08:00 pm'

Here's my function

function showTimeIntervals(value) {
  let result = value.interval.split(","); 
  let start = "";
  let timeNotation = '';
  let time = '';
  for(let i in result) {
    let hr = moment(result[i], 'HH:mm').format('HH');
    let min = moment(result[i], 'HH:mm').format('mm');
    hr = (hr != 0) ? parseInt(hr, 10) : '';
    min = (min != 0) ? parseInt(min, 10) : '';
    if(hr != 0) {
      time = hr;
      timeNotation = 'hour';
      start = moment(value.startTime, 'hh:mm a').subtract(hr, 'hour');
    } else {
      time = min;
      timeNotation = 'minutes';
      start = moment(value.startTime, 'hh:mm a').subtract(min, 'minutes');
    }
  }
  var end = moment(value.endTime, 'hh:mm a');
  if(end < start)
    end = end.add(1, 'd');
  var finalResult = [];
  var current = moment(start);
  while (current <= end) {
    finalResult.push(current.format('hh:mm a'));
    current.add(time, timeNotation);
  }
  return finalResult;
}

But it gives individual time not a duration

 [0] => '02:30 am'
 [1] => '03:00 am'
 [2] => '03:30 am'
 [3] => '04:00 am'
 ......
 [n] => '08:00 pm'

Here's a fiddle -> https://jsfiddle/zm368de9/

Is there an easiest way a moment js can do that?

Share Improve this question edited Jan 16, 2018 at 8:27 Codeblooded Saiyan asked Jan 16, 2018 at 7:49 Codeblooded SaiyanCodeblooded Saiyan 1,4975 gold badges30 silver badges61 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Here is another option to generate intervals using moment.js.

let value = {
  interval: '00:30:00',
  startTime: '03:00:00',
  endTime: '20:00:00'
};

var inputDataFormat = "HH:mm:ss";
var outputFormat = "HH:mm a";

var tmp = moment(value.interval, inputDataFormat);
var dif = tmp - moment().startOf("day");

var startIntervalTime = moment(value.startTime, inputDataFormat).add(-dif, "ms");
var endIntervalTime = moment(value.startTime, inputDataFormat);
var finishTime = moment(value.endTime, inputDataFormat);

function prepareIntervals() {
  var intervals = [];
  
  while(startIntervalTime < finishTime) {
    var format = startIntervalTime.format(outputFormat) + " - " + endIntervalTime.format(outputFormat);
    intervals.push(format);
    startIntervalTime.add(dif, "ms");
    endIntervalTime.add(dif, "ms");
  }
  
  return intervals;
}

console.dir(prepareIntervals());
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.19.1/moment.js"></script>

You are just pushing the current time value and not the entire interval. Just a little bit of modification to the last while loop should do the trick.

Have also changed a few things here

let result = value.interval.split(",")

can be replaced by

result = value.interval

because result.interval does not have a ,.

also, the parison between end and start can be done using moment's in-built isBefore method.

let value = {
  interval: '00:30:00',
  startTime: '03:00:00',
  endTime: '20:00:00'
};

function showTimeIntervals(value) {
  let result = value.interval; 
  let start = "";
  let timeNotation = '';
  let time = '';
  for(let i in result) {
    let hr = moment(result, 'HH:mm').format('HH');
    let min = moment(result, 'HH:mm').format('mm');
    hr = (hr != 0) ? parseInt(hr, 10) : '';
    min = (min != 0) ? parseInt(min, 10) : '';
    if(hr != 0) {
      time = hr;
      timeNotation = 'hour';
      start = moment(value.startTime, 'hh:mm a').subtract(hr, 'hour');
    } else {
      time = min;
      timeNotation = 'minutes';
      start = moment(value.startTime, 'hh:mm a').subtract(min, 'minutes');
    }
  }
  var end = moment(value.endTime, 'hh:mm a');
  if(end.isBefore(start))
    end = end.add(1, 'd');
  var finalResult = [];
  var current = moment(start);
  while (current <= end) {
    currentInterval=current.format('hh:mm a') + ' - '; //This will add the start of interval
    current.add(time, timeNotation);
    currentInterval+=current.format('hh:mm a'); //This will add end of interval
    finalResult.push(currentInterval); //Add the plete interval to your result
  }
  return finalResult;
}

console.log(showTimeIntervals(value))
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.19.1/moment.js"></script>

Hope it helps!!

本文标签: javascriptDisplay time duration based on start and end date with interval using moment jsStack Overflow