Javascript减去彼此的时间跨度

时间:2011-07-25 15:51:50

标签: javascript time timespan

假设我的时间跨度为{"start_time":"8:30", "end_time":"18:00"}

我想减去{"start_time":"12:30", "end_time":"14:00"}等其他时间范围,并得到以下结果:
[{"start_time":"8:30", "end_time":"12:30"},{"start_time":"14:00", "end_time":"18:00"}]

编辑:我还要求减去其他时间跨度,例如 [{"start_time":"8:30", "end_time":"12:30"},{"start_time":"14:00", "end_time":"18:00"}] - {"start_time":"16:30", "end_time":"17:00"}应该给我[{"start_time":"8:30", "end_time":"12:30"},{"start_time":"14:00", "end_time":"16:30"},{"start_time":"17:00", "end_time":"18:00"}]

我想知道最好的逻辑方法是什么。

由于

1 个答案:

答案 0 :(得分:1)

function subtract(t1, t2){
     return [{'start_time': t1.start_time, 'end_time': t2.start_time}, 
               {'start_time': t2.end_time, 'end_time': t1.end_time}]
}