Answer to Question #4063 in Python for Cameron

Question #4063
I am trying to write a talk bot program in python. I'm trying to display how old they are in years, and how long untill they will be the next year older. like if 18 and 12 months 30 days old like..

timetillnext=(dont know what goes here)
bday=raw_input("What day were you born?: ")
bmonth=raw_input("And the month?: ")
byear=raw_input("And what about the year?: ")

if byear <= "1990":
byear=21
if bmonth <="May"
bmonth=5
etc,etc,etc.

print ("Wow your already")+byear
print("And only %time in years, months, and days% untill you are %next year older%")

How do I make the program know the current time, so it can give a
print("wow you have only" %yearstill%, %monthstill%, %daystill% "till you are" %currentage%+1)
1
Expert's answer
2011-08-23T13:37:14-0400
You can use the datetime module in Python
e.g.


import datetime


now = datetime.datetime.now()


print
print &quot;Current date and time using str method of datetime object:&quot;
print str(now)


print
print &quot;Current date and time using instance attributes:&quot;
print &quot;Current year: %d&quot; % now.year
print &quot;Current month: %d&quot; % now.month
print &quot;Current day: %d&quot; % now.day
print &quot;Current hour: %d&quot; % now.hour
print &quot;Current minute: %d&quot; % now.minute
print &quot;Current second: %d&quot; % now.second
print &quot;Current microsecond: %d&quot; % now.microsecond


print
print &quot;Current date and time using strftime:&quot;
print now.strftime(&quot;%Y-%m-%d %H:%M&quot;)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS