{"id":17267,"date":"2021-08-26T10:42:46","date_gmt":"2021-08-26T05:12:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=17267"},"modified":"2021-11-22T18:37:11","modified_gmt":"2021-11-22T13:07:11","slug":"python-program-to-calculate-age-in-days-from-date-of-birth","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-calculate-age-in-days-from-date-of-birth\/","title":{"rendered":"Python Program to Calculate Age in Days from Date of Birth"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check Strontio Number or Not.<\/a> datetime module:<\/strong><\/p>\n The datetime module contains numerous classes that can be used to manipulate date and time in both simple and complex ways.<\/p>\n In this, the date is formatted as the year month date (YY, MM, DD).<\/p>\n datetime.today()<\/strong> :<\/strong>The current date\/system date is returned by datetime.today().<\/p>\n To calculate age from date of birth, subtract the date of birth from the current date.<\/p>\n timedelta() function in Python:<\/strong><\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to calculate age in days from the given Date of Birth.<\/p>\n Approach:<\/strong><\/p>\n Note:<\/strong> If we include the timedelta() function we get age in time including microseconds.<\/p>\n If you want age only in days then remove the timedelta() function import only datetime() function.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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. datetime module: The datetime module contains numerous classes that can be used to manipulate date and time in both simple and complex ways. In this, …<\/p>\n
\nGiven the Date of Birth and task is to calculate the corresponding age in days.<\/p>\nGiven Date of Birth = (2000, 3, 14)<\/pre>\n
The age in days and time for the Given DOB = 7823 days, 14:16:13.409557<\/pre>\n
Given Date of Birth = (1999, 5, 16)<\/pre>\n
The age in days and time for the Given DOB = 8126 days, 14:14:30.074853<\/pre>\n
Program to Calculate Age in Days from Date of Birth<\/h2>\n
\n
Method #1: Using the datetime Module (Static Input)<\/h3>\n
\n
# Import datetime(), timedelta() functions from datetime module using import keyword.\r\nfrom datetime import datetime, timedelta\r\n# Give the date of birth as static input in the format (YY, MM, DD) using datetime() function\r\n# and store it in a variable.\r\ngvn_DOB = datetime(1999, 5, 16)\r\n# Get the current\/today date using datetime.today() function and store it in\r\n# another variable.\r\ncurrent_date = datetime.today()\r\n# Subtract the given date of birth from the current date to get the age in days\r\n# and store it in another variable.\r\nage_in_days = current_date - gvn_DOB\r\n# Print the age in days and time from the given date of birth.\r\nprint(\"The age in days and time for the Given DOB = \", age_in_days)\r\n<\/pre>\n
The age in days and time for the Given DOB = 8126 days, 14:14:30.074853<\/pre>\n
Method #2: Using the datetime Module (User Input)<\/h3>\n
\n
# Import datetime(), timedelta() functions from datetime module using import keyword.\r\nfrom datetime import datetime, timedelta\r\n# Give the year, month, day as user input using map (), int(), split() functions \r\n#and store them separately in three different variables.\r\nyr,mont,dy= map(int,input(\"Enter year ,month ,day separated by spaces = \").split())\r\n#Convert the year, month, day to date of birth using datetime() module and store it in another variable.\r\ngvn_DOB=datetime(yr, mont, dy)\r\n# Get the current\/today date using datetime.today() function and store it in\r\n# another variable.\r\ncurrent_date = datetime.today()\r\n# Subtract the given date of birth from the current date to get the age in days\r\n# and store it in another variable.\r\nage_in_days = current_date - gvn_DOB\r\n# Print the age in days and time from the given date of birth.\r\nprint(\"The age in days and time for the Given DOB = \", age_in_days)\r\n<\/pre>\n
Enter year ,month ,day separated by spaces = 2003 7 19\r\nThe age in days and time for the Given DOB = 6601 days, 14:47:41.427259<\/pre>\n
\n