How do I convert a datetime to date? – Dev

The best answers to the question “How do I convert a datetime to date?” in the category Dev.

QUESTION:

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

ANSWER:

From the documentation:

datetime.datetime.date()

Return date object with same year, month and day.

ANSWER:

Use the date() method:

datetime.datetime.now().date()

ANSWER:

You can convert a datetime object to a date with the date() method of the date time object, as follows:

<datetime_object>.date()

ANSWER:

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()