user46646 user46646 - 3 years ago 131
Python Question

How to get current time in Python?

What is the module/method used to get current time?

Answer Source
>>> import datetime
>>> datetime.datetime.now()
datetime(2009, 1, 6, 15, 8, 24, 78915)

And just the time:

>>> datetime.datetime.time(datetime.datetime.now())
datetime.time(15, 8, 24, 78915)

The same but slightly more compact:

>>> datetime.datetime.now().time()

See the documentation for more info.

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

>>> from datetime import datetime

Then remove the leading datetime. from all the above.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download