Python datetime.datetime?

Just a quick one whilst we're picking up some tips and tricks whilst using Python packages.

The datetime module comes in handy when formatting dates, returning the date time at runtime, figure out time zones, and much more.

Typically it does not require an install as it's part of the build in standard python library but we do have to import it when running in an environment.

Python
import datetime

It'll do the job but when trying to get the date time we would have to write

Plain text
datetime.datetime.now()

This always threw me off, as both the module and class are the same name (e.g. module.class.method : datetime.datetime.now() )

Quickly changing your import to

Python
from datetime import datetime 

we can now call on it by writing

Plain text
datetime.now()

the standard aliasing will work too so changing it to

Python
from datetime import datetime as dt

allows you to call the date time as

Plain text
dt.now()

Albeit, not my preference.

Author:
Robbin Vernooij
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2026 The Information Lab