How do I convert a datetime to date?

PythonDatetime

Python Problem Overview


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

Python Solutions


Solution 1 - Python

Use the date() method:

datetime.datetime.now().date()

Solution 2 - Python

From the documentation:

>datetime.datetime.date() > > Return date object with same year, month and day.

Solution 3 - Python

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

Solution 4 - Python

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

<datetime_object>.date()

Solution 5 - Python

Answer updated to Python 3.7 and more

Here is how you can turn a date-and-time object

(aka datetime.datetime object, the one that is stored inside models.DateTimeField django model field)

into a date object (aka datetime.date object):

from datetime import datetime

#your date-and-time object
# let's supposed it is defined as
datetime_element = datetime(2020, 7, 10, 12, 56, 54, 324893)

# where
# datetime_element = datetime(year, month, day, hour, minute, second, milliseconds)

# WHAT YOU WANT: your date-only object
date_element = datetime_element.date()

And just to be clear, if you print those elements, here is the output :

print(datetime_element)

> 2020-07-10 12:56:54.324893


print(date_element)

> 2020-07-10

Solution 6 - Python

you could enter this code form for (today date & Names of the Day & hour) : datetime.datetime.now().strftime('%y-%m-%d %a %H:%M:%S')

'19-09-09 Mon 17:37:56'

and enter this code for (today date simply): datetime.date.today().strftime('%y-%m-%d') '19-09-10'

for object : datetime.datetime.now().date() datetime.datetime.today().date() datetime.datetime.utcnow().date() datetime.datetime.today().time() datetime.datetime.utcnow().date() datetime.datetime.utcnow().time()

Solution 7 - Python

import time
import datetime

# use mktime to step by one day
# end - the last day, numdays - count of days to step back
def gen_dates_list(end, numdays):
  start = end - datetime.timedelta(days=numdays+1)
  end   = int(time.mktime(end.timetuple()))
  start = int(time.mktime(start.timetuple()))
  # 86400 s = 1 day
  return xrange(start, end, 86400)

# if you need reverse the list of dates
for dt in reversed(gen_dates_list(datetime.datetime.today(), 100)):
    print datetime.datetime.fromtimestamp(dt).date()

Solution 8 - Python

Solved: AttributeError: 'Series' object has no attribute 'date'

You can use as below,

df["date"] = pd.to_datetime(df["date"]).dt.date

where in above code date contains both date and time (2020-09-21 22:32:00), using above code we can get only date as (2020-09-21)

Solution 9 - Python

I use data.strftime('%y-%m-%d') with lambda to transfer column to date

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionniklasfiView Question on Stackoverflow
Solution 1 - PythonarsView Answer on Stackoverflow
Solution 2 - Pythonuser395760View Answer on Stackoverflow
Solution 3 - PythontzotView Answer on Stackoverflow
Solution 4 - PythonFurbeenatorView Answer on Stackoverflow
Solution 5 - PythonTms91View Answer on Stackoverflow
Solution 6 - Pythonkamran26View Answer on Stackoverflow
Solution 7 - PythonSerenityView Answer on Stackoverflow
Solution 8 - PythonManjula DeviView Answer on Stackoverflow
Solution 9 - PythonWei Chun ChangView Answer on Stackoverflow