Age in days – Python Program to Calculate Age in Days from Date of Birth

Age in days: In the previous article, we have discussed Python Program to Check Strontio Number or Not.
Given the Date of Birth and task is to calculate the corresponding age in days. Python code to show age in years, months days, Python age in days, Python code for date of birth, Python age calculator date time, If you carefully observe in the content you will get it.

datetime module:

Your age in days: The datetime module contains numerous classes that can be used to manipulate date and time in both simple and complex ways.

In this, the date is formatted as the year month date (YY, MM, DD).

datetime.today() :The current date/system date is returned by datetime.today().

To calculate age from date of birth, subtract the date of birth from the current date.

timedelta() function in Python:

The Python timedelta() function is part of the datetime library and is commonly used for calculating date differences. It can also be used for date manipulation in Python. It is one of the simplest methods for manipulating dates.

Examples:

Example1:

Input:

Given Date of Birth = (2000, 3, 14)

Output:

The age in days and time for the Given DOB =  7823 days, 14:16:13.409557

Example2:

Input:

Given Date of Birth = (1999, 5, 16)

Output:

The age in days and time for the Given DOB = 8126 days, 14:14:30.074853

Program to Calculate Age in Days from Date of Birth

Below are the ways to calculate age in days from the given Date of Birth.

Method #1: Using the datetime Module (Static Input)

Approach:

  • Import datetime(), timedelta() functions from datetime module using import keyword.
  • Give the date of birth as static input in the format (YY, MM, DD) using datetime() function and store it in a variable.
  • Get the current/today date using datetime.today() function and store it in another variable.
  • Subtract the given date of birth from the current date to get the age in days and store it in another variable.
  • Print the age in days and time from the given date of birth.
  • The Exit of the Program.

Note: If we include the timedelta() function we get age in time including microseconds.

If you want age only in days then remove the timedelta() function import only datetime() function.

Below is the implementation:

# Import datetime(), timedelta() functions from datetime module using import keyword.
from datetime import datetime, timedelta
# Give the date of birth as static input in the format (YY, MM, DD) using datetime() function
# and store it in a variable.
gvn_DOB = datetime(1999, 5, 16)
# Get the current/today date using datetime.today() function and store it in
# another variable.
current_date = datetime.today()
# Subtract the given date of birth from the current date to get the age in days
# and store it in another variable.
age_in_days = current_date - gvn_DOB
# Print the age in days and time from the given date of birth.
print("The age in days and time for the Given DOB = ", age_in_days)

Output:

The age in days and time for the Given DOB =  8126 days, 14:14:30.074853

Method #2: Using the datetime Module (User Input)

Approach:

  • Import datetime(), timedelta() functions from datetime module using import keyword.
  • Give the year, month, day as user input using map (), int(), split() functions and store them separately in three different variables.
  • Convert the year, month, day to date of birth using datetime() module and store it in another variable.
  • Get the current/today date using datetime.today() function and store it in another variable.
  • Subtract the given date of birth from the current date to get the age in days and store it in another variable.
  • Print the age in days and time from the given date of birth.
  • The Exit of the Program.

Below is the implementation:

# Import datetime(), timedelta() functions from datetime module using import keyword.
from datetime import datetime, timedelta
# Give the year, month, day as user input using map (), int(), split() functions 
#and store them separately in three different variables.
yr,mont,dy= map(int,input("Enter year ,month ,day separated by spaces = ").split())
#Convert the year, month, day to date of birth using datetime() module and store it in another variable.
gvn_DOB=datetime(yr, mont, dy)
# Get the current/today date using datetime.today() function and store it in
# another variable.
current_date = datetime.today()
# Subtract the given date of birth from the current date to get the age in days
# and store it in another variable.
age_in_days = current_date - gvn_DOB
# Print the age in days and time from the given date of birth.
print("The age in days and time for the Given DOB = ", age_in_days)

Output:

Enter year ,month ,day separated by spaces = 2003 7 19
The age in days and time for the Given DOB = 6601 days, 14:47:41.427259

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Test Yourself:

  1. Write a python program to calculate your age in days between today and date of birth?
  2. Python program to calculate age in years?
  3. Python calculate age between two dates?
  4. Calculate age in months python?
  5. Calcualte age from date of birth python pandas?
  6. Python calculate age between two dates?
  7. Calculate age in months python?
  8. Python program to calculate age in days?
  9. How to calculate age in days in python
  10. Python calculate age from date of birth?
  11. Python program to calculate age from date of birth?
  12. Python program to calculate age in years months and days?
  13. Python calculate age from date of birth?

Related Posts On: