How do I convert a datetime to date?

How do I convert a datetime.datetime object (e.g., the return value of datetime.datetime.now()) to a datetime.date object in Python?


Solution 1:

Use the date() method:

datetime.datetime.now().date()

Solution 2:

From the documentation:

datetime.datetime.date()

Return date object with same year, month and day.

Solution 3:

You use the datetime.datetime.date() method:

datetime.datetime.now().date()

Obviously, the expression above can (and should IMHO :) be written as:

datetime.date.today()