How do I get the current time in Python?

datetime, python, time

Solution

Use `datetime`:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
>>> print(now)
2009-01-06 15:08:24.789150

For just the clock time without the date:

>>> now.time()
datetime.time(15, 8, 24, 78915)
>>> print(now.time())
15:08:24.789150

To save typing, you can import the `datetime` object from the `datetime` module:

>>> from datetime import datetime

Then remove the prefix `datetime.` from all of the above.

Problem

How do I get the current time in Python?

Original source

Related problems