Calculate the difference in time spans using Javascript

If I have a time range

{"start_time":"8:30", "end_time":"18:00"}
, and I want to subtract another time range like
{"start_time":"12:30", "end_time":"14:00"}
, the result would be:
[{"start_time":"8:30", "end_time":"12:30"},{"start_time":"14:00", "end_time":"18:00"}]

Additionally, if I need to subtract more time ranges sequentially e.g.

[{"start_time":"8:30", "end_time":"12:30"},{"start_time":"14:00", "end_time":"18:00"}]
-
{"start_time":"16:30", "end_time":"17:00"}
, the expected output should be
[{"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 looking for the most effective logical approach to achieve this. Any suggestions?

Thank you

Answer №1

function calculateDifference(timepoint1, timepoint2){
     return [{'initial_time': timepoint1.initial_time, 'final_time': timepoint2.initial_time}, 
               {'initial_time': timepoint2.final_time, 'final_time': timepoint1.final_time}]
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In JavaScript, it is not possible to retrieve the maximum or minimum value of an array

Hey there! I'm having some trouble finding the minimum and maximum values in an array. Can you help me figure out what's going on with my code? var repeated, studentsArray = [], marksArray = []; while (repeated !== 'n' && r ...

Waiting in iOS UI Automation for the web view to be prepared and displayed

In my quest to develop an iOS UI Automation javascript with Instruments for automating the process of taking a screenshot in my iOS app, I have turned to the handy tool known as Snapshot. A crucial part of my app involves a webview, and I am keen on captu ...

Remove the attribute from the element in the ng-repeat loop

Here is my ng-repeat code snippet: <th ng-repeat="field in tableFields" translate="{{field.headerTitle | translate}}" ts-criteria="{{field.sortable ? field.fieldKey : null}}"> <label class="i-checks" ng-if="field.isCheckbox"> & ...

Exiting shell sessions may involve NPM, Supervisor, and Nohup

I'm currently using Node LTS and I have a script that needs to run continuously. Even after disconnecting from SSH, I want it to keep running. I recently came across a node package called supervisor which is supposed to restart the process if it crash ...

In JavaScript, what causes a reference error when reading an undeclared variable, but returns undefined when accessing an undeclared object property

When I run the code below and print this.property2 in the console, the JS engine returns undefined. However, if I simply print property2 instead, the compiler throws a reference error. In order to avoid this error, I need to declare the variable property2, ...

Unable to expand the width of the video element

So I have encountered a situation like this The video displayed does not fully expand to the width of its containing element. This is what my HTML structure looks like - <div class="webcam"/> <div class="control-container"> </div> ...

Send the data from a div to a different page using PHP

Is there a way to pass the value displayed in DIV id to another page? The data is retrieved from another page and shown within the given code. <div id="res" style="float: left; Padding-left: 3px; padding-right:13px;line-height: 15px;"> ...... ...

Tips for safely sanitizing strings containing special characters such as "\r\n"

Strings are coming in the following formats: "\\r\\n " "\\r \\n" "\\n \\r " These strings are being retrieved from a third party API. Before storing them in the database, I need to conv ...

Issue with finding module in TypeScript component import during Jest test

Despite having the correct path to my styled objects, I'm puzzled by the error message that has popped up: Error: Cannot find module '../../shared/models' from 'Astronaut.tsx' import { moonHoldings } from '../../shared/models ...

When a function inside React uses this.props, it won't trigger the corresponding function

I am currently developing a Checklist using React and MaterialUI that consists of two components. One component is responsible for managing the data, while the other component allows for editing the data. However, I have encountered an issue where the func ...

Tips for changing an array of objects with identical key names?

Here's an array of objects: p =[ { "object1": "value", }, { "object2": "value", }, { "object1": "value", }, { "object3": "value", }, { "object4": "value", }, { "object1": "value", }, { "object3": "value" } ]; I'm loo ...

Transitioning from using JQuery functions to AngularJS

I have been working on converting a user list/table that I originally created using Razor and JQuery into AngularJS. One of the key functionalities of this table was the ability to toggle the display of additional user details. The table consisted of two s ...

Adjust the background color of the dropdown when toggled

I want to add a background color to my dropdown menu when it's activated. In the demo, the dropdown content overlaps with the text behind it. Currently, I have a scrollspy feature that applies a background color to the nav bar and dropdown menu when s ...

Am I making a mistake in my implementation of sending Socket.io messages to a designated room?

Upon a user joining, I alter their ID to a shortened random code. This change is made to utilize the id as a visible session ID on the front-end. I mention this to prevent confusion regarding non-standard socket IDs and their relation to potential issues. ...

The highlight_row function seems to be delayed, as it does not trigger on the initial onClick but works on subsequent clicks. How can I ensure it works properly right from the first click?

I developed an application using the Google JavaScript Maps API to calculate distances between addresses. I've encountered a bug where the row in the table is not highlighted on the first click, requiring a second click for the highlighting to take ef ...

Arranging rows according to an array value containing column (PHP/MySQL/jQuery)

Initially, I am seeking a solution using PHP/MySQL and JS/jQuery, but I am unsure of the most effective approach for handling larger datasets (40,000+). Consider a scenario where you have a database and wish to enable sorting of a table based on a column ...

Populating SVG element with information extracted from JSON

Hi there! I'm dealing with a JSON file that contains data for 249 countries, each with their respective iso codes. My goal is to declare the iso code as a variable named 'iso' and the number of visitors as a variable named 'visitors&apo ...

optimal method for integrating various javascript components within a single-page application

Looking to create a single page application with the following architecture in mind: Each page or widget serving as an independent, reusable module for this project and potentially beyond A core application that can utilize these external modules, facili ...

What is the best way to display a pop-up notification once a user has successfully logged in?

I'm currently developing a website using asp.net core mvc for an assignment. The website features identity server for user authentication. I am attempting to display a pop-up alert once a user enters the correct login credentials and clicks the Login ...

Ways to trigger a JavaScript notification

I consider myself experienced in scripting, but I can't seem to figure this out. I'm trying to trigger a function on a button click event, so I decided to start by testing the buttonclick event with a basic window.alert. I wrote the following htm ...