如何从 json 输出计算可用时间?

我在对象中有这种带有开始和结束时间属性的 json。


var meetings = [

  {

    id: '1',

    start_time: "2020-11-15T08:30:00+00:00",

    end_time: "2020-11-15T14:15:00+00:00"

  },

  {

    id: '2',

    start_time: "2020-11-15T19:30:00+00:00",

    end_time: "2020-11-15T20:30:00+00:00"

  },

];

所以两次会议第一个:08:30 - 14:15 第二个:19:30-20:30


有 24 小时的可用工作时间,因此:


start_time = '00:00:00';

end_time = '23:59:00';

但我需要的格式是


var start_time = '2020-11-15T00:00:00+00:00';

var end_time = '2020-11-15T23:59:00+00:00';

我如何根据已经召开的会议计算可用时间?因此,因为第一次会议是从 08:30 - 14:15 和第二个会议是 19:30-20:30,所以我需要具有以下结果和可用时间的 json:



[

  {

    start_time:'2020-11-15T00:00:00+00:00',

    end_time:'2020-11-15T08:30:00+00:00'

  },


  {

    start_time:'2020-11-15T14:15:00+00:00',

    end_time:'2020-11-15T19:30:00+00:00'

  },


  {

    start_time:'2020-11-15T20:30:00+00:00',

    end_time:'2020-11-15T23:59:00+00:00'

  }

]


largeQ
浏览 111回答 1
1回答

慕容708150

你可以这样做:var meetings = [  {    id: '1',    start_time: "2020-11-15T08:30:00+00:00",    end_time: "2020-11-15T14:15:00+00:00"  },  {    id: '2',    start_time: "2020-11-15T19:30:00+00:00",    end_time: "2020-11-15T20:30:00+00:00"  },  {    id: '3',    start_time: "2020-11-15T20:50:00+00:00",    end_time: "2020-11-15T21:30:00+00:00"  },  {    id: '4',    start_time: "2020-11-15T22:30:00+00:00",    end_time: "2020-11-15T22:40:00+00:00"  },];var startTime = '2020-11-15T00:00:00+00:00';var endTime = '2020-11-15T23:59:00+00:00';let availableHours = [];for(let index in meetings){    if(index != meetings.length - 1){        index == 0 ? availableHours.push({start_time: startTime, end_time: meetings[index]['start_time'] }) : 0;        availableHours.push({start_time: meetings[index].end_time, end_time: meetings[+index + 1]['start_time'] });    } else {        availableHours.push({start_time: meetings[index].end_time, end_time: endTime })    }    }console.log(meetings)console.log(availableHours)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript