Day 006 of the 100 Days of Python challenge calculates how long a person has lived from a date entered in MM/DD/YYYY format.
Project Overview
The program uses datetime.strptime() to parse the user’s birthdate and datetime.now() for the current date. It reports the elapsed years, months, days, hours, minutes, and seconds.
birthdate = datetime.strptime(birth_input, "%m/%d/%Y")
now = datetime.now()
time_in_between = now - birthdate
Years and months require additional calendar logic. The program compares the current month and day with the birth month and day, adjusts when the current day has not reached the birth day, and borrows a year when necessary.
Input Validation
The calculator runs inside a while True loop. A user can enter q to exit, while incorrectly formatted dates are caught with ValueError. The program then displays an error and asks again.
What This Project Teaches
Day 006 introduces the standard library’s datetime module, parsing date strings, subtracting dates, formatting dates for display, loops, and exception handling. It also illustrates that calendar calculations need more care than simple arithmetic because months have different lengths.
Running the Project
cd 100daysofpython/day006
python main.py
View the Day 006 source code on GitHub.
