
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to find difference between current time and given time
When it is required to find the difference between the current time and a given time, a method can be defined, that takes the hours, minutes, and seconds as parameter. It then calculates the difference between two given times.
Below is a demonstration of the same −
Example
def difference_time(h_1, m_1, h_2, m_2): t_1 = h_1 * 60 + m_1 t_2 = h_2 * 60 + m_2 if (t_1 == t_2): print("The times are the same") return else: diff = t_2-t_1 hours = (int(diff / 60)) % 24 mins = diff % 60 print(hours, ":", mins) if __name__ == "__main__": print("The difference between times are given below :") difference_time(13,20,11, 49) difference_time(17, 11, 9, 59) difference_time(21, 4, 11, 34)
Output
The difference between times are given below : 23 : 29 17 : 48 15 : 30
Explanation
A method named difference_time is defined that takes three parameters.
The times are converted into minutes.
When the timings are different, they are subtracted, and the hours and the minutes are displayed as output.
In the main method, this method is called by passing different parameter.
The output is displayed on the console.
- Related Questions & Answers
- Difference Between Time Sharing and Real-Time Operating System
- Program to find nearest time by reusing same digits of given time in python
- Java Program to Display current Date and Time
- How to find time difference using Python?
- Difference between two given time periods in C++
- Java Program to display Current Time in another Time Zone
- Getting current time in Python
- C++ Program to print current Day, Date and Time
- Find the time which is palindromic and comes after the given time in Python
- Java Program to Get Current Date/Time
- How to get current date and time in Python?
- How to print current date and time using Python?
- C++ Program to Calculate Difference Between Two Time Period
- Current Date and Time in Perl
- Difference between compile-time polymorphism and runtime polymorphism
Advertisements