Get the Current Date and Time in Python: datetime.now, date.today

Modified: | Tags: Python, Date and time

In Python, you can get the current date and time using the time and datetime modules from the standard library.

The time.time() function returns the current Unix time (epoch time), and the datetime.datetime.now() function returns the current datetime object.

See the following article for basic information about the datetime module.

Get the current Unix time (epoch time): time.time()

Unix time (also called epoch time) represents the number of seconds that have elapsed since 00:00:00 UTC on January 1, 1970.

Use time() from the time module to get the current Unix time as a float.

import time

ut = time.time()

print(ut)
# 1549281692.9876952

print(type(ut))
# <class 'float'>

You can also use time() to measure the elapsed time of a process.

To convert between Unix time and datetime objects, see the following article.

Get the current datetime object: datetime.datetime.now()

Use datetime.now() from the datetime module to get a datetime object representing the current date and time.

import datetime

dt_now = datetime.datetime.now()

print(dt_now)
# 2019-02-04 21:04:15.412854

print(type(dt_now))
# <class 'datetime.datetime'>

You can convert datetime objects to strings in any desired format, including ISO 8601.

print(dt_now.strftime('%Y年%m月%d日 %H:%M:%S'))
# 2019年02月04日 21:04:15

print(type(dt_now.strftime('%Y年%m月%d日 %H:%M:%S')))
# <class 'str'>

print(dt_now.isoformat())
# 2019-02-04T21:04:15.412854

print(type(dt_now.isoformat()))
# <class 'str'>

Additionally, you can access the year, month, day, hour, minute, second, and microsecond attributes.

print(dt_now.year)
# 2019

print(dt_now.month)
# 2

print(dt_now.day)
# 4

print(dt_now.hour)
# 21

print(dt_now.minute)
# 4

print(dt_now.second)
# 15

print(dt_now.microsecond)
# 412854

print(type(dt_now.year))
# <class 'int'>

Handle the timezone

Be cautious when dealing with time zones.

By default, a naive object is returned with the tzinfo attribute set to None, representing the local time. In this example, the environment is set to Japan Standard Time (JST), so the returned value is the current time in JST.

dt_now = datetime.datetime.now()

print(dt_now)
# 2019-02-04 21:04:15.412854

print(type(dt_now))
# <class 'datetime.datetime'>

print(dt_now.tzinfo)
# None

Specify a timezone object as an argument to datetime.now() to obtain an aware object that considers the time zone.

dt_now_utc_aware = datetime.datetime.now(datetime.timezone.utc)

print(dt_now_utc_aware)
# 2019-02-04 12:04:15.561748+00:00

print(dt_now_utc_aware.tzinfo)
# UTC
dt_now_jst_aware = datetime.datetime.now(
    datetime.timezone(datetime.timedelta(hours=9))
)

print(dt_now_jst_aware)
# 2019-02-04 21:04:15.591827+09:00

print(dt_now_jst_aware.tzinfo)
# UTC+09:00

The datetime.utcnow() function returns a naive datetime object representing the current UTC time, with its tzinfo attribute set to None.

dt_now_utc_naive = datetime.datetime.utcnow()

print(dt_now_utc_naive)
# 2019-02-04 12:04:15.621472

print(dt_now_utc_naive.tzinfo)
# None

Get today's date object: datetime.date.today()

Use date.today() from the datetime module to get today's date object.

d_today = datetime.date.today()

print(d_today)
# 2019-02-04

print(type(d_today))
# <class 'datetime.date'>

date.today() returns the local date.

Although date objects do not have a tzinfo attribute, you can still account for time zones when creating them.

First, create a datetime object with the desired time zone using the datetime.now() function with arguments or the datetime.utcnow() function for UTC. Then, convert it to a date object using the date() method.

d_today_utc = datetime.datetime.utcnow().date()

print(d_today_utc)
# 2019-02-04

print(type(d_today_utc))
# <class 'datetime.date'>

In this example, both results are the same. However, if the time difference crosses a date boundary, the results will differ.

Get the current time object

While the datetime module includes a time type, it does not provide a direct way to get the current time as a time object (as of Python 3.13).

You can create a datetime object and then convert it to a time object using the time() method.

t_now = datetime.datetime.now().time()

print(t_now)
# 21:04:15.782546

print(type(t_now))
# <class 'datetime.time'>

print(t_now.tzinfo)
# None

The time() method ignores the tzinfo attribute of the original datetime object.

dt_now_utc_aware = datetime.datetime.now(datetime.timezone.utc)

print(dt_now_utc_aware)
# 2019-02-04 12:04:15.838464+00:00

print(dt_now_utc_aware.tzinfo)
# UTC

print(dt_now_utc_aware.time())
# 12:04:15.838464

print(dt_now_utc_aware.time().tzinfo)
# None

If you want to preserve the tzinfo attribute, use the timetz() method.

print(dt_now_utc_aware.timetz())
# 12:04:15.838464+00:00

print(dt_now_utc_aware.timetz().tzinfo)
# UTC

Related Categories

Related Articles