993

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

martineau
  • 119,623
  • 25
  • 170
  • 301
niklasfi
  • 15,245
  • 7
  • 40
  • 54
  • 39
    If you just need this for `datetime.datetime.now()`, please note that there is a method `datetime.date.today()`. – Thierry J. May 06 '16 at 01:04
  • if you have already imported datetime e.g. `from datetime import datetime` you can just add date `from datetime import datetime, date` – Josh Feb 18 '19 at 13:06

10 Answers10

1516

Use the date() method:

datetime.datetime.now().date()
Flimm
  • 136,138
  • 45
  • 251
  • 267
ars
  • 120,335
  • 23
  • 147
  • 134
  • 24
    To get in UTC which can be very helpful --> `datetime.datetime.utcnow()` and correspondingly `datetime.datetime.utcnow().date()` – Nick Brady Mar 07 '16 at 22:51
  • 3
    Thanks @SeanColombo, current date in a particular timezone should be `datetime.datetime.now(pytz.timezone('US/Pacific')).date()` – Monica For CEO Oct 25 '19 at 23:24
  • 4
    datetime.datetime.utcnow() is deprecated see https://docs.python.org/3/library/datetime.html#datetime.datetime replace with datetime.now(timezone.utc). The OP is actually after date so this is moot. – hum3 Aug 28 '20 at 15:47
  • 2
    Given `from datetime import datetime, date, timezone` and one using a timezone with a non-zero offset, then `datetime.now(timezone.utc).date()` **can** be different from `datetime.now().date()` (the latter being also available as `date.today()`). – tzot Oct 19 '20 at 12:02
166

From the documentation:

datetime.datetime.date()

Return date object with same year, month and day.

Community
  • 1
  • 1
76

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()
tzot
  • 92,761
  • 29
  • 141
  • 204
  • 2
    to be fair, this answer (using `.today()`) is the most pythonic for the stated example question. – Dannid Oct 17 '16 at 18:39
  • 2
    **If you use today, be careful when comparing a date.** `today()` has an _hour_ component too. so if you do: `losdat = datetime.datetime.strptime(losdatstr, '%d%m%Y')`and then `if losdat < datetime.datetime.today():` it will always be true because `losdat`will have a time component of midnight which will fall before the timestamp of `today()` – DDecoene Mar 28 '17 at 08:48
  • 6
    @DennisDecoene: why use `datetime.datetime.today()` instead of `datetime.date.today()`, when one wants a `datetime.date` object? – tzot Mar 29 '17 at 12:04
  • @tzot Yes that was also what I wanted to point out but was unclear about. – DDecoene Mar 29 '17 at 13:41
62

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

<datetime_object>.date()
Furbeenator
  • 8,106
  • 4
  • 46
  • 54
12

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

kamran26
  • 129
  • 1
  • 3
  • OP wanted to get ```datetime.date``` object, and not string, which ```strftime``` would return (ref: https://docs.python.org/3/library/datetime.html#datetime.date.strftime). – Grzegorz Skibinski Sep 10 '19 at 09:15
11

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

Tms91
  • 3,456
  • 6
  • 40
  • 74
6
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()
Serenity
  • 35,289
  • 20
  • 120
  • 115
1
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)

Manjula Devi
  • 151
  • 2
  • 8
0

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

-3

If you are using pandas then this can solve your problem:

Lets say that you have a variable called start_time of type datetime64 in your dataframe then you can get the date part like this:

df.start_time.dt.date
Orn Arnar
  • 133
  • 1
  • 10