I am learning python(beginner) and have been set a task to write a program to find difference in days between user's date of birth and now, in Python. I know how to work out the time now (datetime.now) and how to ask the user to input something(raw_input). However, these answers are in different formats, so there is no way to subtract one from another. Is there a way of converting them to the same format, or would slicing(not sure if that's the right terminology-so baton[0:3]-the first 4 charcters in the word baton) and then turning into integers, and then subtracting work. there have been some other answers on stack overflow, using strptime(not sure what this is). However, these posts talk about subtracting two dates that are already in the same format and are known by the program(i.e , no use-input involved). Here are these posts: How do I calculate number of days betwen two dates using Python?, python date difference in minutes and How do I find the time difference between two datetime objects in python? I hope that this information is enough that somone may be able to help. I would be really grateful if anyone was able to help me. Thanks!
Asked
Active
Viewed 1,970 times
1
-
1Use datetime.strptime. Time to read up – sshashank124 May 04 '14 at 13:04
-
Would it be on the python website? – user3601352 May 04 '14 at 13:05
-
https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime – sshashank124 May 04 '14 at 13:06
-
And: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior – sshashank124 May 04 '14 at 13:06
-
related: [Converting string into datetime](http://stackoverflow.com/q/466345/4279) – jfs May 04 '14 at 18:01
-
related: [Python date string to date object](http://stackoverflow.com/q/2803852/4279) – jfs May 04 '14 at 18:02
1 Answers
0
It seems your question is: "how do I convert user input into a datetime (or date) object?". You could ask the date in a fixed format e.g., "yyyy-mm-dd":
from datetime import datetime
# read birth day in yyyy-mm-dd format
while True:
datestr = raw_input("Input your birthday (yyyy-mm-dd): ")
try:
birthday = datetime.strptime(datestr, "%Y-%m-%d")
except ValueError as e:
print("error: %s\nTry again." % (e,))
else:
break
number_of_days_since_birthday = (datetime.utcnow() - birthday).days
Note: It ignores the difference in hours -- and timezones.
If you know the hour of your birthday and the place (timezone); you could get a more precise result:
from datetime import datetime
import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal
# assume you are in the same timezone where you were burn
# otherwise specify the timezone explicitly e.g.,
# timezone = pytz.timezone('Europe/London')
timezone = get_localzone()
# read birth day
while True:
datestr = raw_input("Input your birthday (yyyy-mm-dd HH:MM:SS): ")
try:
birthday = datetime.strptime(datestr, "%Y-%m-%d %H:%M:%S")
except ValueError as e:
print("error: %s\nTry again." % (e,))
else:
break
# assume no DST transitions during birth
birthday = timezone.localize(birthday)
# ignoring leap seconds
birthday_timedelta = (datetime.now(pytz.utc) - birthday)
number_of_days_since_birthday = birthday_timedelta.total_seconds() / 86400.
print(number_of_days_since_birthday)

jfs
- 399,953
- 195
- 994
- 1,670