Python Program to demonstrate the time arithmetic

Time arithmetic involves performing mathematical operations on time values such as calculating durations, finding differences between times, or adding time intervals. This is essential for applications like scheduling, data analysis, and time tracking. Python provides built-in functions like strptime() and divmod() to handle time arithmetic operations with both 12-hour and 24-hour formats.

Understanding Time Formats

Python supports two main time formats ?

  • 12-hour format: Uses AM/PM notation (e.g., 2:30:15 PM)

  • 24-hour format: Uses 24-hour notation (e.g., 14:30:15)

Syntax

For 12-hour format ?

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

Format specifiers for 12-hour time ?

  • %I Hours (01-12)

  • %M Minutes (00-59)

  • %S Seconds (00-59)

  • %p AM/PM indicator

For 24-hour format ?

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

Format specifiers for 24-hour time ?

  • %H Hours (00-23)

  • %M Minutes (00-59)

  • %S Seconds (00-59)

The divmod() function syntax ?

divmod(dividend, divisor)

Example 1: Detailed Time Duration Calculation

This example calculates the detailed duration between two times in 12-hour format, breaking it down into days, hours, minutes, and seconds ?

from datetime import datetime

# Define two times in 12-hour format
time1 = datetime.strptime("12:11:14am", "%I:%M:%S%p")
time2 = datetime.strptime("8:09:30pm", "%I:%M:%S%p")

# Calculate the duration
total_duration = time2 - time1

# Extract hours, minutes, and seconds using divmod()
hours, remainder = divmod(total_duration.seconds, 3600)
minutes, seconds = divmod(remainder, 60)

print(f"Time duration: {total_duration.days} days, {hours} hours, {minutes} minutes, {seconds} seconds")
Time duration: 0 days, 19 hours, 58 minutes, 16 seconds

Example 2: Time Arithmetic with Both Formats

This example demonstrates time arithmetic using both 12-hour and 24-hour formats ?

from datetime import datetime

# Time arithmetic with 12-hour format
time1_12h = datetime.strptime('12:36:56 PM', '%I:%M:%S %p')
time2_12h = datetime.strptime('8:04:22 AM', '%I:%M:%S %p')
difference_12h = time1_12h - time2_12h

print('Time difference in 12-hour format:', difference_12h)

# Time arithmetic with 24-hour format
time1_24h = datetime.strptime('23:39:16', '%H:%M:%S')
time2_24h = datetime.strptime('13:52:21', '%H:%M:%S')
difference_24h = time1_24h - time2_24h

print('Time difference in 24-hour format:', difference_24h)
Time difference in 12-hour format: 4:32:34
Time difference in 24-hour format: 9:46:55

Key Points

  • strptime() converts time strings into datetime objects for calculations

  • divmod() helps extract hours, minutes, and seconds from total seconds

  • Subtracting datetime objects returns a timedelta object

  • Both 12-hour and 24-hour formats support time arithmetic operations

Conclusion

Python's datetime module provides powerful tools for time arithmetic. Use strptime() to parse time strings and divmod() to extract specific time components from calculations.

Updated on: 2026-03-27T07:07:27+05:30

297 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements