Dada dos veces en formato de string “HH:MM”. Aquí ‘H’ muestra las horas y ‘M’ muestra los minutos. Tienes que encontrar la diferencia en el mismo formato de string entre estas dos strings. Pero ambas strings dadas deben seguir estos casos.
1) El tiempo 1 será menor o igual que el tiempo
2 2) Las horas serán posibles de 0 a 23.
3) Los minutos serán posibles de 0 a 59
Ejemplos:
Input : s1 = "14:00"; s2 = "16:45"; Output : result = "2:45" Input : s1 = "1:04" s2 = "13:05" Output : result = "12:01"
La idea es primero quitar el colon. Después de eliminar los dos puntos, la diferencia horaria se puede calcular usando matemáticas simples.
C++
// C++ program to find difference between two // given times. #include <bits/stdc++.h> using namespace std; // remove ':' and convert it into an integer int removeColon(string s) { if (s.size() == 4) s.replace(1, 1, ""); if (s.size() == 5) s.replace(2, 1, ""); return stoi(s); } // Main function which finds difference string diff(string s1, string s2) { // change string (eg. 2:21 --> 221, 00:23 --> 23) int time1 = removeColon(s1); int time2 = removeColon(s2); // difference between hours int hourDiff = time2 / 100 - time1 / 100 - 1; // difference between minutes int minDiff = time2 % 100 + (60 - time1 % 100); if (minDiff >= 60) { hourDiff++; minDiff = minDiff - 60; } // convert answer again in string with ':' string res = to_string(hourDiff) + ':' + to_string(minDiff); return res; } // Driver program to test above functions int main() { string s1 = "14:00"; string s2 = "16:45"; cout << diff(s1, s2) << endl; return 0; }
Java
/*package whatever //do not write package name here */ import java.io.*; class GFG { // Java program to find difference between two // given times. // remove ':' and convert it into an integer static int removeColon(String s) { if (s.length() == 4) s = s.substring(0,1) + s.substring(2); if (s.length() == 5) s = s.substring(0,2) + s.substring(3); return Integer.valueOf(s); } // Main function which finds difference static String diff(String s1, String s2) { // change string (eg. 2:21 --> 221, 00:23 --> 23) int time1 = removeColon(s1); int time2 = removeColon(s2); // difference between hours int hourDiff = time2 / 100 - time1 / 100 - 1; // difference between minutes int minDiff = time2 % 100 + (60 - time1 % 100); if (minDiff >= 60) { hourDiff++; minDiff = minDiff - 60; } // convert answer again in string with ':' String res = String.valueOf(hourDiff) + ':' + String.valueOf(minDiff); return res; } // Driver Code public static void main(String args[]) { String s1 = "14:00"; String s2 = "16:45"; System.out.println(diff(s1, s2)); } } // This code is contributed by shinjanpatra
Python3
# Python 3 program to find difference between two # given times. # Remove ':' and convert it into an integer def removeColon(s): if (len(s) == 4): s = s[:1] + s[2:] if (len(s) == 5): s = s[:2] + s[3:] return int(s) # Main function which finds difference def diff(s1, s2): # Change string # (eg. 2:21 --> 221, 00:23 --> 23) time1 = removeColon(s1) time2 = removeColon(s2) # Difference between hours hourDiff = time2 // 100 - time1 // 100 - 1; # Difference between minutes minDiff = time2 % 100 + (60 - time1 % 100) if (minDiff >= 60): hourDiff += 1 minDiff = minDiff - 60 # Convert answer again in string with ':' res = str(hourDiff) + ':' + str(minDiff) return res # Driver code s1 = "14:00" s2 = "16:45" print(diff(s1, s2)) # This code is contributed by ApurvaRaj
C#
// C# program to find difference between two // given times. using System; class GFG { // remove ':' and convert it into an integer static int removeColon(string s) { if (s.Length == 4) s = s.Substring(0, 1) + s.Substring(2); if (s.Length == 5) s = s.Substring(0, 2) + s.Substring(3); return Convert.ToInt32(s); } // Main function which finds difference static string diff(string s1, string s2) { // change string (eg. 2:21 --> 221, 00:23 --> 23) int time1 = removeColon(s1); int time2 = removeColon(s2); // difference between hours int hourDiff = time2 / 100 - time1 / 100 - 1; // difference between minutes int minDiff = time2 % 100 + (60 - time1 % 100); if (minDiff >= 60) { hourDiff++; minDiff = minDiff - 60; } // convert answer again in string with ':' string res = Convert.ToString(hourDiff) + ":" + Convert.ToString(minDiff); return res; } // Driver Code public static void Main(string[] args) { string s1 = "14:00"; string s2 = "16:45"; Console.WriteLine(diff(s1, s2)); } } // This code is contributed by phasing17
Javascript
<script> // JavaScript program to find difference between two // given times. // remove ':' and convert it into an integer function removeColon( s) { if (s.length == 4) s= s.replace(":", ""); if (s.length == 5) s= s.replace(":", ""); return parseInt(s); } // Main function which finds difference function diff( s1, s2) { // change string (eg. 2:21 --> 221, 00:23 --> 23) time1 = removeColon(s1); time2 = removeColon(s2); // difference between hours hourDiff = parseInt(time2 / 100 - time1 / 100 - 1); // difference between minutes minDiff = parseInt(time2 % 100 + (60 - time1 % 100)); if (minDiff >= 60) { hourDiff++; minDiff = minDiff - 60; } // convert answer again in string with ':' res = (hourDiff).toString() + ':' + (minDiff).toString(); return res; } // Driver program to test above functions s1 = "14:00"; s2 = "16:45"; console.log(diff(s1, s2)); // This code is contributed by ukasp. </script>
Producción:
2:45
Este artículo es una contribución de Harshit Agrawal . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA