Python Program To Calculate Age Of A Person

Chapter: Python Last Updated: 22-09-2023 04:54:25 UTC

Program:

            /* ............... START ............... */
                

from datetime import datetime

def calculate_age(birthdate):
    # Get the current date
    current_date = datetime.now()
    
    # Calculate the age by subtracting the birthdate from the current date

    age = current_date.year - birthdate.year - ((current_date.month,
              current_date.day) < (birthdate.month, birthdate.day))
    

    return age

# Input the birthdate in the format YYYY-MM-DD
birthdate_str = input("Enter your birthdate (YYYY-MM-DD): ")

try:
    # Convert the input string to a datetime object
    birthdate = datetime.strptime(birthdate_str, "%Y-%m-%d")
    
    # Calculate and print the age
    age = calculate_age(birthdate)
    print(f"Your age is {age} years.")
except ValueError:
    print("Invalid date format. Please enter the date in YYYY-MM-DD format.")

                /* ............... END ............... */
        

Output

Enter your birthdate (YYYY-MM-DD): 1990-05-15
Your age is 33 years.


In this example, the user entered the birthdate "1990-05-15," and the program 
calculated the age as 33 years based on the current date.

Notes:

  • Here's how this program works:
  • It takes user input for their birthdate in the format "YYYY-MM-DD."
  • Then It converts the input string to a datetime object using strptime.
  • In next step it calculates the age using the difference between the current date and the birthdate.
  • Finally it prints the calculated age.
  • Make sure to input the birthdate in the specified format (e.g., "1990-05-15"). The program will handle invalid date formats and raise a ValueError in such cases.

Tags

Python program to calculate age of a person #How to calculate someones age using python? #Age calculator in Python

Similar Programs Chapter Last Updated
Python Program To Check Whether Element Present In Set Or Not Example Python 04-10-2023
Python Program To Find Maximum And Minimum Number In A Set Python 04-10-2023
Python Program To Check Symmetric Matrix Python 04-10-2023
Python Program To Find Subsets Of A Set Python 04-10-2023
Python Program To Find Power Set Of A Set Python 04-10-2023
Remove All Duplicates From List Python Python 04-10-2023
Python Program To Find Symmetric Difference Of Two Sets Python 27-09-2023
Python Program To Find Common Item From Two Set Python 27-09-2023
Python Program To Get Unique Values From A List Python 27-09-2023
Python Encode And Decode String With Key Python 24-09-2023
Python Simple Encrypt Decrypt String Python 24-09-2023
Python Format String To Specific Length Python 24-09-2023
Python Code To Check If String Contains Substring Python 24-09-2023
Python Program To Find Most Repeated Word In A String Python 23-09-2023
Split String Into Words Python Python 23-09-2023
Remove All Punctuation Python Python 23-09-2023
Python Program To Reverse An Array Python 23-09-2023
Python Program To Find Number Of Palindrome In A String Python 23-09-2023
Python Program To Find Longest Common Substring Python 23-09-2023
Python Program To Find Number Of Days In A Given Month And Year Python 22-09-2023
Python Code To Get Day Of Week Python 22-09-2023
Python Convert String To Date Without Time Python 22-09-2023
Python Program To Print Current Date And Time In Format dd/mm/yyyy Python 22-09-2023
Python Program To Find Working Days In A Month Python 19-09-2023
Python Code To Change Date Format Python 16-09-2023
Python Program To Calculate Number Of Days Between Two Dates Python 16-09-2023
Python Program To Calculate Age In Years Months And Days Python 16-09-2023
Python Program To Schedule A Job To Run After A Certain Amount Of Time Python 10-08-2023
Python Program To Schedule A Job To Run Randomly Once A Day Python 10-08-2023
Python Program To Schedule A Job To Run Every Hour Python 10-08-2023

1 2 3 4