This Python script calculates the time difference between an input time and the current time, displaying the result in a human-readable format.
- 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
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)
Time since vacation to Bali: 1 year, 2 months, 3 days, 4 hours, 5 minutes ago
python time_difference_calculator.pyYou 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}")The repository includes test scripts to verify the functionality:
python test_time_calculator.pypython demo_bali_vacation.pycalculate_time_difference(input_time): Main function that calculates and formats the time differenceparse_input_time(time_str): Parses various date/time string formats into datetime objectsmain(): Entry point for the interactive command-line interface
- 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")
- 1 year = 365 days
- 1 month = 30 days (for calculation purposes)
- These approximations may cause slight variations in results for very long periods
To exit the interactive mode, type 'quit' when prompted for input.