← Go Back

How to Get the Current Date and/or Time

The datetime.datetime.now() standard function returns the current date and time:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2023, 12, 28, 20, 1, 34, 419931)

You can access the date values individually:

>>> now.year
2023
>>> now.month
12
>>> now.day
28

And the time values:

>>> now.hour
20
>>> now.minute
1
>>> now.second
34
>>> now.microsecond
419931

You might also use now.date() or now.time() to get the date or the time separately.

datetime dates


🐍 You might also find interesting: