# Python Program to demonstrate the time arithmetic

The process of conducting mathematical operations on time values is referred to as time arithmetic. Adding or subtracting time durations, determining the difference between two-time values, or determining the average of a group of time values are a few examples of time arithmetic. Some of the applications are used to demonstrate time arithmetic such as time tracker, data analysis, and time scheduling. In Python, we have time built-in functions i.e. strptime() and divmod() that can be used to demonstrate the time arithmetic. There are two ways of time formats that are 12 hours and 24 hours.

Let’s take an example to understand the duration of two different times.

The duration of two different times in a 12-hour format −

The time difference between 12:14:11PM – 08:08:20AM is 19 hours, 54 minutes, and 9 seconds.

The duration of two different times in a 24-hour format −

13:14:11 – 17:15:12 =  1 hours, 42 minutes, and 56 seconds.


Let’s see how to calculate the time arithmetic.

## Syntax

The following syntax used in the examples are −

strptime( 'Time_format_in_12_hours', '%I:%M:%S %p' )


The strptime method in Python is used to set the time format. The following representation of the 12 hours time format −

• %I − Hours

• %M − Minutes

• %S − Seconds

• %p − AM/PM

strptime( 'Time_format_in_24_hours', '%H:%M:%S' )


This is the time format of 24 hours and the following representation are −

• %H − Hours

• %M − Minutes

• %S − Seconds

divmod( r, q )


The divmod() is a predefined method used in Python that accepts two parameters −

• r − The r represents the remainder.

• q − The q represents the quotient.

## Example 1

In the following example, we will start the program by importing the module datetime in Python. Then initialize the two variables namely- ‘time1’ and ‘time2’ which define the time format of 12 hours. Then find the time duration by subtracting the two different times and store it in the variable ‘Total_duration’. Next, we create the two variables namely- ‘hrs’ and ‘mint’ to calculate the time duration divided by 3600 by using the predefined method divmod(). Then create the two together variables namely- ‘mint’ and ‘sec’ to calculate the exact minutes and seconds. Finally, we are printing the variable with the help of the variable ‘hrs’, ‘mint’, and ‘sec’.

from datetime import datetime
time1 = datetime.strptime( "12:11:14am", "%I:%M:%S%p" )
time2 = datetime.strptime( "8:09:30pm", "%I:%M:%S%p" )
Total_duration = time2 - time1
hrs, rem = divmod( Total_duration.seconds, 3600 )
mint, sec = divmod( rem, 60 )
print( f"The time duration of 12 hours format: {Total_duration.days} days, {hrs} hours, {mint} minutes, {sec} seconds" )


## Output

The time duration of 12 hours format: 0 days, 19 hours, 58 minutes, 16 seconds


## Example 2

In the following example, we will start the time arithmetic program by importing the module datetime in Python. Then we take two variables namely- ‘time1’ and ‘time2’ to define the time formats by using ‘strptime()’ method. Then subtract the times i.e. time2 - time1 to get the duration of the time and store it in the variable ‘difference’. Finally, we are printing the result with the help of the variable ‘difference’.

#time arithmetic
from datetime import datetime
# 12hours format
time1 = datetime.strptime( '12:36:56 PM', '%I:%M:%S %p' )
time2 = datetime.strptime( '8:04:22 AM', '%I:%M:%S %p' )
difference = time1 - time2
print('Time difference in 12-hour format:', difference)
# 24hours format
time1 = datetime.strptime( '23:39:16', '%H:%M:%S' )
time2 = datetime.strptime( '13:52:21', '%H:%M:%S' )
difference1 = time1 - time2
print( 'Time difference in 24-hour format:', difference1 )


## Output

Time difference in 12-hour format: 4:32:34
Time difference in 24-hour format: 9:46:55


## Conclusion

We understood the difference between the two examples. We saw the strptime() methods set the two different time formats i.e. 12 hours and 24 hours. To find the duration of two times, we subtract the one time by another time. The divmod() method calculates the remainder and quotient of minutes and seconds respectively. Therefore, this way we demonstrate the time arithmetic.

Updated on: 01-Jun-2023

85 Views