简体   繁体   English

Javascript相互减去时间跨度

[英]Javascript subtract time spans from each other

Suppose I have a time span {"start_time":"8:30", "end_time":"18:00"} 假设我有一个时间跨度{"start_time":"8:30", "end_time":"18:00"}

I want to subtract other time span like {"start_time":"12:30", "end_time":"14:00"} and get a result of: 我想减去其他时间跨度,例如{"start_time":"12:30", "end_time":"14:00"}并得到以下结果:
[{"start_time":"8:30", "end_time":"12:30"},{"start_time":"14:00", "end_time":"18:00"}]

EDIT: I will also require to subtract other time spans next eg [{"start_time":"8:30", "end_time":"12:30"},{"start_time":"14:00", "end_time":"18:00"}] - {"start_time":"16:30", "end_time":"17:00"} which should give me [{"start_time":"8:30", "end_time":"12:30"},{"start_time":"14:00", "end_time":"16:30"},{"start_time":"17: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"}]

I am wondering what is the best logical way to do it. 我想知道什么是最好的逻辑方法。

Thanks 谢谢

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

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

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