使用时刻js以间隔显示基于开始和结束日期的持续时间

时间:2018-01-16 07:49:01

标签: javascript datetime momentjs

我希望在给定startTime,endTime和interval的情况下显示持续时间数组。

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

示例输出必须是具有给定间隔

的持续时间数组

-startTime减去间隔,所以新的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'

这是我的功能

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

但它给个人时间而不是持续时间

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

这是一个小提琴 - &gt; https://jsfiddle.net/zm368de9/

js能做到这一点的最简单的方法是什么?

2 个答案:

答案 0 :(得分:3)

这是使用moment.js生成间隔的另一种选择。

&#13;
&#13;
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());
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您只是推送当前时间值而不是整个时间间隔。只需对最后一个while循环进行一些修改即可。

这里也改变了一些事情

  

让result = value.interval.split(&#34;,&#34;)

可以替换为

result = value.interval

因为result.interval没有,

此外,endstart之间的比较可以使用内置的isBefore方法进行。

&#13;
&#13;
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 complete interval to your result
  }
  return finalResult;
}

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

希望它有所帮助!!