J Min
J Min

Reputation: 55

When do you use double datetime.datetime in python?

This timestamp is used for bug logging.

import os.path
import datetime

timestamp = datetime.datetime.now().strftime('%Y-%m-%d')

Tried to search but was unable to find anything related to this. I would like to know when do you use double datetime method?

Upvotes: 2

Views: 291

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36604

The first datetime refers to the library, and the second to one of its modules.

Which should you use? It depends how you imported it. If you just imported the library, like this:

import datetime

you will need to write the library name again, like this: datetime.datetime.now(). If you imported the module, like this:

from datetime import datetime

you imported the module, so you can just use datetime.now() afterwards. Both are absolutely equal, it's up to you to decide which you choose.

Upvotes: 6

Related Questions