Extracting information *Getting an error* Please help!


#1

My code :

from datetime import datetime
now = datetime.now()
print datetime.now()
now = now.year
print now.year
now = now.month
print now.month
now = now.day
print now.day


#2

here:

now = now.year

now has a value of 2017, 2017.year will throw an error, integer 2017 doesn't have year attribute

this lines:

now = now.year
now = now.month
now = now.day

are wrong, they overwrite now with the wrong value throwing attributes error


#3

so what should my code look like?
add the value variable now = 2017 and then 2017.year?


#4

no? after doing this:

now = datetime.now()

now variable contains the current time and date, so you can just print now.year directly without doing this:

now = now.year

which will overwrite the current time and date with the year (integer)


#5

that applies for month and date too?


#6

oh wait. Got it! Thanks alot mate :slight_smile:


#7

yes, now is not a string. its a datetime object, so once you overwrite, you are in trouble. Good you understand :slight_smile:


#8