简体   繁体   中英

Javascript subtract time spans from each other

Suppose I have a time span {"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":"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"}]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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