Let’s calculate your age. And specifically, years, months, and days. Here’s how:
Age calculations
import datetime birth_day = int(input("Day of birth: ")) birth_month = int(input("Month of birth: ")) birth_year = int(input("Year of birth: ")) day = int(datetime.date.today().day) month = int(datetime.date.today().month) year = int(datetime.date.today().year) if birth_year > year: age = year - birth_year else: age = (year - birth_year) - 1 b = abs(month - birth_month) c = abs(day - birth_day) print("Your age is:", age, "years", b, "months", c, "days")
Thr script asks about the day, month, and year of your birthday. The output is the exact age of yours. With some simple modifications, you can use the script for other datetime calculations.