The datetime.now() method from Python's datetime module returns a datetime object representing the current local date and time, including year, month, day, hour, minute, second, and microsecond. By default, it produces a naive object (without timezone information) in the format YYYY-MM-DD HH:MM:SS.microseconds.
To use this function, import the datetime class and call the method:
from datetime import datetime
# Get current date and time
now = datetime.now()
print(now) # Output: 2025-06-08 14:30:45.123456You can extract specific components using attributes or format the output using strftime():
Attributes: Access
.year,.month,.day,.hour,.minute,.second, and.microsecond.Formatting: Use
now.strftime('%Y-%m-%d %H:%M:%S')for custom string formats.Timezones: Pass a timezone object (e.g.,
datetime.now(timezone.utc)) to get a timezone-aware result.Difference: It is similar to
datetime.today(), butnow()accepts an optionaltzparameter for timezone handling.
Use datetime:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
>>> str(now)
'2009-01-06 15:08:24.078915'
For just the clock time without the date:
>>> now.time()
datetime.time(15, 8, 24, 78915)
>>> str(now.time())
'15:08:24.078915'
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.
Videos
Use datetime:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
>>> str(now)
'2009-01-06 15:08:24.078915'
For just the clock time without the date:
>>> now.time()
datetime.time(15, 8, 24, 78915)
>>> str(now.time())
'15:08:24.078915'
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.
Use time.strftime():
>>> from time import gmtime, strftime
>>> strftime("%Y-%m-%d %H:%M:%S", gmtime())
'2009-01-05 22:14:39'