Skip to content

This Python script calculates the time difference between an input time and the current time, displaying the result in a human-readable format.

License

Notifications You must be signed in to change notification settings

alamaby/python-time-difference-calculator

Repository files navigation

Time Difference Calculator

This Python script calculates the time difference between an input time and the current time, displaying the result in a human-readable format.

Features

  • Calculates time differences in years, months, days, hours, and minutes
  • Shows whether the input time is in the past ("X ago") or future ("X from now")
  • Supports multiple date/time input formats
  • Provides customizable labels for time events
  • Handles edge cases like times less than a minute apart

Usage

Run the script and enter dates in any of these formats:

  • YYYY-MM-DD HH:MM:SS (e.g., 2025-06-15 10:30:00)
  • YYYY-MM-DD (e.g., 2025-06-15)
  • MM/DD/YYYY HH:MM:SS (e.g., 06/15/2025 10:30:00)
  • MM/DD/YYYY (e.g., 06/15/2025)
  • YYYY-MM-DDTHH:MM:SS (ISO format, e.g., 2025-06-15T10:30:00)
  • YYYY-MM-DDTHH:MM:SSZ (ISO format with Z, e.g., 2025-06-15T10:30:00Z)

Example Output

Time since vacation to Bali: 1 year, 2 months, 3 days, 4 hours, 5 minutes ago

How to Run

Interactive Mode

python time_difference_calculator.py

Programmatic Usage

You can also import and use the functions in your own code:

from time_difference_calculator import calculate_time_difference
import datetime

# Calculate time difference for a specific date
my_date = datetime.datetime(2025, 6, 15, 10, 0, 0)
result = calculate_time_difference(my_date)
print(f"Time difference: {result}")

Testing

The repository includes test scripts to verify the functionality:

Run Basic Tests

python test_time_calculator.py

Run Demo Example

python demo_bali_vacation.py

Functions

  • calculate_time_difference(input_time): Main function that calculates and formats the time difference
  • parse_input_time(time_str): Parses various date/time string formats into datetime objects
  • main(): Entry point for the interactive command-line interface

Example Use Cases

  • Tracking how long ago an event happened (e.g., "Time since vacation to Bali")
  • Countdown to future events (e.g., "Time until birthday")
  • Age calculation (e.g., "How many years since founding")
  • Duration tracking (e.g., "How long since last maintenance")

Approximations Used

  • 1 year = 365 days
  • 1 month = 30 days (for calculation purposes)
  • These approximations may cause slight variations in results for very long periods

Exit

To exit the interactive mode, type 'quit' when prompted for input.

About

This Python script calculates the time difference between an input time and the current time, displaying the result in a human-readable format.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages