1

I want to appoint datetime.datetime to a variable but I came across this mistake. How can I fix it?

from datetime import datetime

liste = {'latest_time': datetime.datetime(2000, 1, 5, 0, 0), 'earliest_time': datetime.datetime(2017, 12, 4, 0, 0)}
print(liste['latest_time'])

liste = {'latest_time': datetime.datetime(2000, 1, 5, 0, 0), 'earliest_time': datetime.datetime(2017, 12, 4, 0, 0)}

AttributeError: type object 'datetime.datetime' has no attribute 'datetime'

2 Answers 2

2

Without changing your code, you can change just the import statement to:

import datetime

Within the datetime module is an object also called datetime. Your current code is importing the datetime object to the main namespace. So that object class lives in your main namespace just as if you had written:

class datetime(object):
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Fixed my issue with from datetime import datetime, timezone
0

#You can simply try below code..

from datetime import datetime, timedelta 

from time import mktime

yesterday = datetime.now() - timedelta(days=1)

print(yesterday)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.