newbPyth
newbPyth

Reputation: 31

Defining datetime to get year

Can someone help me out with this

    print("Created On:",issue.fields.created) #Created On: 2021-07-16T11:07:54.000+0000
    datef = issue.fields.created.strip()
    dataspl,lixo = datef.split("T")
    datetimeobject = datetime.strptime(dataspl, '%Y-%m-%d')
    datetime_object = datetime.strftime(datetimeobject, '%d/%m/%Y')
    print(datetime_object) #16/07/2021

    if datetime_object.year == something
    AttributeError: 'str' object has no attribute 'year'

Why is this appening? isnt the datetime_object weel define?

thanks

Upvotes: 0

Views: 25

Answers (2)

DeepSpace
DeepSpace

Reputation: 81594

You should be getting the year attribute from the datetime object, not from the string representation. Try datetimeobject.year.

PS, avoid having both datetimeobject variable (a datetime object) and datetime_object variable. These names are too similiar, especially when datetime_object is lying about its type (it's a string, not a datetime object. Even you got confused).

Upvotes: 1

ForceBru
ForceBru

Reputation: 44828

datetime.strftime(datetimeobject, '%d/%m/%Y') returns a string, not a datetime object, so the datetime_object in datetime_object = datetime.strftime(datetimeobject, '%d/%m/%Y') is a string that "has no attribute 'year'"

Upvotes: 0

Related Questions