1

I am trying to create a program in python that tells you how many days you have lived for. The code I have at the moment is:

import datetime
from datetime import timedelta

year = int(input('Enter the year'))
month = int(input('Enter the month'))
day = int(input('Enter the day'))
date1 = datetime.date(year, month, day)

now = datetime.datetime.now().date()

days = now - date1
print days

At the moment it prints the number the number of days and then 0:00:00. For example: 5914 days, 0:00:00. Does anyone know how to get rid of the 0:00:00?

René Hoffmann
  • 2,766
  • 2
  • 20
  • 43
T. Green
  • 323
  • 10
  • 20
  • 1
    So you just want `(now - date1).days`? The result of the calculation is a [`timedelta`](https://docs.python.org/2/library/datetime.html#datetime.timedelta) object. – jonrsharpe Sep 21 '15 at 14:44
  • 1
    `>>> print 'days : {}'.format(days.days)` – Mazdak Sep 21 '15 at 14:46
  • 2
    reading through this [doc](https://docs.python.org/2/library/datetime.html) would help greatly – taesu Sep 21 '15 at 14:46
  • related: [Age from birthdate in python](http://stackoverflow.com/q/2217488/4279) – jfs Sep 21 '15 at 19:24
  • you could use `from datetime import date; now = date.today()`. You could use `birthday = datetime.strptime(raw_input('Enter birthday as YYYY-MM-DD: '), '%Y-%m-%d')`, to get the birthday at once. – jfs Sep 21 '15 at 19:27

1 Answers1

5

days is a timedelta object, so just do print days.days

Nathaniel
  • 770
  • 7
  • 14