GeeksforGeeks App
Open App
Browser
Continue

# Time Difference between given times in HH:MM:SS format

Given 2 times ‘st‘ and ‘et‘ in HH: MM: SS format. The task is to print the time difference between st and et in HH:MM: SS format

Examples:

Input: st = 13:50:45, et = 14:55:50
Output: 01:05:05
Explanation: The time gap is 1 hour 5 minutes and 5 seconds.

Input: st = 12:00:00, et = 24:00:00
Output: 12:00:00
Explanation: The time gap is of 12 hours.

Approach: The task can be solved by converting both the given times in ‘seconds‘ format & then find the absolute difference between the two. Then convert this absolute difference into HH:MM: SS format
Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `class` `Solution {``public``:``    ``// Function to find the time difference``    ``long` `long` `int` `getTimeInSeconds(string str)``    ``{` `        ``vector<``int``> curr_time;``        ``istringstream ss(str);``        ``string token;` `        ``while` `(getline(ss, token, ``':'``)) {` `            ``curr_time.push_back(stoi(token));``        ``}` `        ``long` `long` `int` `t = curr_time[0] * 60 * 60``                          ``+ curr_time[1] * 60``                          ``+ curr_time[2];` `        ``return` `t;``    ``}` `    ``// Function to convert seconds back to hh::mm:ss``    ``// format``    ``string convertSecToTime(``long` `long` `int` `t)``    ``{``        ``int` `hours = t / 3600;``        ``string hh = hours < 10 ? ``"0"` `+ to_string(hours)``                               ``: to_string(hours);``        ``int` `min = (t % 3600) / 60;``        ``string mm = min < 10 ? ``"0"` `+ to_string(min)``                             ``: to_string(min);``        ``int` `sec = ((t % 3600) % 60);``        ``string ss = sec < 10 ? ``"0"` `+ to_string(sec)``                             ``: to_string(sec);``        ``string ans = hh + ``":"` `+ mm + ``":"` `+ ss;``        ``return` `ans;``    ``}` `    ``// Function to find the time gap``    ``string timeGap(string st, string et)``    ``{` `        ``long` `long` `int` `t1 = getTimeInSeconds(st);``        ``long` `long` `int` `t2 = getTimeInSeconds(et);` `        ``long` `long` `int` `time_diff``            ``= (t1 - t2 < 0) ? t2 - t1 : t1 - t2;` `        ``return` `convertSecToTime(time_diff);``    ``}``};` `// Driver Code``int` `main()``{` `    ``string st = ``"13:50:45"``, et = ``"14:55:50"``;``    ``Solution ob;``    ``cout << ob.timeGap(st, et) << ``"\n"``;` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``class` `GFG {` `    ``// Function to find the time difference``    ``static` `int` `getTimeInSeconds(String str) {` `        ``String[] curr_time = str.split(``":"``);``        ``int` `t = Integer.parseInt(curr_time[``0``]) * ``60` `* ``60` `+ Integer.parseInt(curr_time[``1``]) * ``60``                ``+ Integer.parseInt(curr_time[``2``]);` `        ``return` `t;``    ``}` `    ``// Function to convert seconds back to hh::mm:ss``    ``// format``    ``static` `String convertSecToTime(``int` `t) {``        ``int` `hours = t / ``3600``;``        ``String hh = hours < ``10` `? ``"0"` `+ Integer.toString(hours)``                ``: Integer.toString(hours);``        ``int` `min = (t % ``3600``) / ``60``;``        ``String mm = min < ``10` `? ``"0"` `+ Integer.toString(min)``                ``: Integer.toString(min);``        ``int` `sec = ((t % ``3600``) % ``60``);``        ``String ss = sec < ``10` `? ``"0"` `+ Integer.toString(sec)``                ``: Integer.toString(sec);``        ``String ans = hh + ``":"` `+ mm + ``":"` `+ ss;``        ``return` `ans;``    ``}` `    ``// Function to find the time gap``    ``static` `String timeGap(String st, String et) {` `        ``int` `t1 = getTimeInSeconds(st);``        ``int` `t2 = getTimeInSeconds(et);` `        ``int` `time_diff = (t1 - t2 < ``0``) ? t2 - t1 : t1 - t2;` `        ``return` `convertSecToTime(time_diff);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[]) {` `        ``String st = ``"13:50:45"``, et = ``"14:55:50"``;``        ``System.out.println(timeGap(st, et));``    ``}``}` `// This code is contributed by saurabh_jaiswal.`

## Python3

 `# Python 3 program for the above approach` `# Function to find the time difference``def` `getTimeInSeconds(``str``):` `    ``curr_time ``=` `str``.split(``':'``)` `    ``t ``=` `(``int``(curr_time[``0``]) ``*` `60` `*` `60``         ``+` `int``(curr_time[``1``]) ``*` `60``         ``+` `int``(curr_time[``2``]))` `    ``return` `t` `# Function to convert seconds back to hh::mm:ss``# format``def` `convertSecToTime(t):` `    ``hours ``=` `t ``/``/` `3600``    ``if` `hours < ``10``:``        ``hh ``=` `"0"` `+` `str``(hours)``    ``else``:``        ``hh ``=` `str``(hours)``    ``min` `=` `(t ``%` `3600``) ``/``/` `60``    ``if` `min` `< ``10``:``        ``mm ``=` `"0"` `+` `str``(``min``)``    ``else``:``        ``mm ``=` `str``(``min``)``    ``sec ``=` `((t ``%` `3600``) ``%` `60``)``    ``if` `sec < ``10``:``        ``ss ``=` `"0"` `+` `str``(sec)``    ``else``:``        ``ss ``=` `str``(sec)``    ``ans ``=` `hh ``+` `":"` `+` `mm ``+` `":"` `+` `ss``    ``return` `ans` `# Function to find the time gap``def` `timeGap(st,  et):` `    ``t1 ``=` `getTimeInSeconds(st)``    ``t2 ``=` `getTimeInSeconds(et)` `    ``if` `(t1 ``-` `t2 < ``0``):``        ``time_diff ``=` `t2 ``-` `t1``    ``else``:``        ``time_diff ``=` `t1 ``-` `t2` `    ``return` `convertSecToTime(time_diff)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``st ``=` `"13:50:45"``    ``et ``=` `"14:55:50"` `    ``print``(timeGap(st, et))` `    ``# This code is contributed by ukasp.`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;``public` `class` `GFG {` `    ``// Function to find the time difference``    ``static` `int` `getTimeInSeconds(String str) {` `        ``String[] curr_time = str.Split(``':'``);``        ``int` `t = Int32.Parse(curr_time[0]) * 60 * 60 + Int32.Parse(curr_time[1]) * 60``                ``+ Int32.Parse(curr_time[2]);` `        ``return` `t;``    ``}` `    ``// Function to convert seconds back to hh::mm:ss``    ``// format``    ``static` `String convertSecToTime(``int` `t) {``        ``int` `hours = t / 3600;``        ``String hh = hours < 10 ? ``"0"` `+ hours.ToString()``                ``: hours.ToString();``        ``int` `min = (t % 3600) / 60;``        ``String mm = min < 10 ? ``"0"` `+ min.ToString()``                ``: min.ToString();``        ``int` `sec = ((t % 3600) % 60);``        ``String ss = sec < 10 ? ``"0"` `+ sec.ToString()``                ``: sec.ToString();``        ``String ans = hh + ``":"` `+ mm + ``":"` `+ ss;``        ``return` `ans;``    ``}` `    ``// Function to find the time gap``    ``static` `String timeGap(String st, String et) {` `        ``int` `t1 = getTimeInSeconds(st);``        ``int` `t2 = getTimeInSeconds(et);` `        ``int` `time_diff = (t1 - t2 < 0) ? t2 - t1 : t1 - t2;` `        ``return` `convertSecToTime(time_diff);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(String []args) {` `        ``String st = ``"13:50:45"``, et = ``"14:55:50"``;``        ``Console.WriteLine(timeGap(st, et));``    ``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``

Output

`01:05:05`

Time complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up