1029

Is there a built-in method for converting a date to a datetime in Python, for example getting the datetime for the midnight of the given date? The opposite conversion is easy: datetime has a .date() method.

Do I really have to manually call datetime(d.year, d.month, d.day)?

7
  • 226
    Not a silly question; date objects should have a .datetime() method; what's silly is that they don't have such a method.
    – Zags
    Commented Oct 14, 2013 at 3:57
  • 26
    @Zags: or datetime.from_date() constructor.
    – jfs
    Commented May 9, 2015 at 21:36
  • 16
    no it shouldn't a date is a subset of datetime (it's even in the name). It's unambiguous what the date is from a datetime. But the other way round, a date is a block of 24h (usually), so it can have many datetimes. What datetime would come from a date? You can't always so 00:00 because what if that time doesn't exist, like for example daylight savings skipped it. Not so easy#
    – dalore
    Commented Mar 1, 2017 at 12:33
  • 7
    @Dalore Daylight savings time is between 1am and 2am to avoid exactly the problem you are describing.
    – Zags
    Commented Jan 25, 2019 at 21:28
  • 4
    @zags it all depends on the timezone source and destination, you can have a daylight savings time change at 1am but that would correspond to a midnight change in some other timezone. and if you're programming which timezone are you using? you could very well end up trying to get a time that doesn't exist. My point still stands, adding a time to a date is not straight forward. Then you also got to consider is your new datetime also timezone aware or naive.
    – dalore
    Commented Feb 4, 2019 at 15:44

12 Answers 12

1289

You can use datetime.combine(date, time); for the time, you create a datetime.time object initialized to midnight.

from datetime import date
from datetime import datetime

dt = datetime.combine(date.today(), datetime.min.time())
12
  • 127
    Thanks. Combined with the fact that time() returns (0,0) I think this comes out the cleanest: datetime.combine(d, time())
    – EMP
    Commented Dec 21, 2009 at 0:38
  • 52
    Just be careful not to let your code get datetime.time() confused with time.time(). Qualified names FTW!
    – Dustin
    Commented Dec 21, 2009 at 1:19
  • 11
    Yes, good point. Fortunately, combine() raises an exception if you pass in a time.time or anything else other than a datetime.time.
    – EMP
    Commented Dec 21, 2009 at 8:36
  • 23
    For midnight, there's a python constant at either datetime.time.min (2.7.1+) or datetime.min.time() (older python)
    – larham1
    Commented Jun 4, 2011 at 23:05
  • 31
    Good solution, but I don't think datetime.min.time() is the cleanest way of getting a 00:00:00 time. That is because what it does is first retrieving the minimum value representable by datetime and then getting its time component. Incidentally, datetime.min = datetime(MINYEAR, 1, 1, tzinfo=None) and has a time of 00:00:00. However, I think it is cleaner to explicitly create a 00:00:00 time either through time.min or time(0, 0, 0, 0). Commented Dec 2, 2013 at 14:02
222

There are several ways, although I do believe the one you mention (and dislike) is the most readable one.

>>> import datetime
>>> t=datetime.date.today()
>>> datetime.datetime.fromordinal(t.toordinal())
datetime.datetime(2009, 12, 20, 0, 0)

>>> datetime.datetime(t.year, t.month, t.day)
datetime.datetime(2009, 12, 20, 0, 0)

>>> datetime.datetime(*t.timetuple()[:-4])
datetime.datetime(2009, 12, 20, 0, 0)

and so forth -- but basically they all hinge on appropriately extracting info from the date object and ploughing it back into the suitable ctor or classfunction for datetime.

6
  • 35
    Very good point. datetime(d.year, d.month, d.day) does seem much more readable than the accepted answer datetime.combine(d, datetime.min.time()).
    – Simon Elms
    Commented Jun 28, 2014 at 12:03
  • 3
    @SimonTewsi: midnight = datetime.datetime.combine(d, datetime.time.min) seems cleaner (suggested by @larham1)
    – jfs
    Commented May 9, 2015 at 21:36
  • 2
    @J. F. Sebastian: It is somewhat cleaner but I agree with Wes Winham in his answer that it requires the reader to know what datetime.time.min represents. Whereas it seems to me that datetime(d.year, d.month, d.day) is more obvious (a datetime without the time component). It's a minor point, though, and using a variable name like "midnight" makes it obvious what the result is even if the reader doesn't know what datetime.time.min represents.
    – Simon Elms
    Commented May 10, 2015 at 23:48
  • 8
    @SimonTewsi: note: datetime(d.year, d.mont, d.day) is not only more obvious, it is faster too.
    – jfs
    Commented Dec 4, 2015 at 11:49
  • Another variant just for fun: datetime.datetime(*(getattr(d, attr) for attr in ("year", "month", "day"))). That way, you can easily modify the list of attributes that matter to you. Hours could be added dynamically for example.
    – Guimoute
    Commented Mar 12, 2022 at 20:31
133

The accepted answer is correct, but I would prefer to avoid using datetime.min.time() because it's not obvious to me exactly what it does. If it's obvious to you, then more power to you. I also feel the same way about the timetuple method and the reliance on the ordering.

In my opinion, the most readable, explicit way of doing this without relying on the reader to be very familiar with the datetime module API is:

from datetime import date, datetime
today = date.today()
today_with_time = datetime(
    year=today.year, 
    month=today.month,
    day=today.day,
)

That's my take on "explicit is better than implicit."

4
  • And a hat tip to Kyle Gibson for the original idea: stackoverflow.com/users/513197/kyle-gibson
    – Wes Winham
    Commented Mar 19, 2014 at 21:25
  • 1
    I agree with your sentiment. All these other answers with timetuple and asterisks and min.time() require research, and the Python way of handling dates and datetimes is a bit cumbersome anyway compared to the ease that Microsoft handles them in VBA. It as simple as wrapping them in hash symbols in that language. But I digress.
    – Bobort
    Commented Oct 27, 2016 at 15:46
  • 2
    This works, but you may need the datetime to be timezone aware. In which case, you can import pytz and then add tzinfo=pytz.timezone('Europe/London'), for example.
    – alstr
    Commented May 21, 2020 at 9:28
  • Since 3.6, simply use < naive datetime >.astimezone() for local timezone adjustments. Since 3.9, handling specific timezones got even easier, just leverage zoneinfo like this: from zoneinfo import ZoneInfo; datetime(year=..., month=..., day=..., tzinfo=ZoneInfo('Europe/London')). No need of external dependencies!
    – manus
    Commented Mar 5 at 16:39
60

You can use the date.timetuple() method and unpack operator *.

args = d.timetuple()[:6]
datetime.datetime(*args)
4
  • 5
    This is useful for my situation. I don't know if it's a date or a datetime I'm being passed, and it's not very pythonic to check which class it is. This method looks like it will work for both datetime and date objects.
    – Gattster
    Commented Oct 14, 2010 at 20:51
  • I used to use this before discovering datetime.combine via @kiamlaluno's answer. I think it's fairly pythonic, especially given constructing a datetime.time object is likely to look something like datetime.time(*map(int,"H:M:S".split(":"))) anyway...
    – Tom
    Commented Jan 29, 2011 at 1:45
  • useful in many other contexts too for example truncating seconds, minutes, or hours by indexing down to as low as [:3] and even lower if you pad with + (n,) as in : dt.datetime(*(dt.datetime.utcnow().timetuple()[:2] + (1, ))) Commented Aug 30, 2021 at 13:10
  • Nice approach, for convenience here's the documentation for timetuple() Commented Sep 28, 2022 at 16:18
14

you can also use

date = datetime.utcnow().date()
dt = datetime.fromisoformat(date.isoformat())

print(dt)

datetime.datetime(2021, 11, 15, 0, 0)

1
  • 2
    You can even add the time as a string in this way: datetime.fromisoformat(date.isoformat() + ' 17:32') Commented Jan 9, 2022 at 10:02
10

Today being 2016, I think the cleanest solution is provided by pandas Timestamp:

from datetime import date
import pandas as pd
d = date.today()
pd.Timestamp(d)

Timestamp is the pandas equivalent of datetime and is interchangable with it in most cases. Check:

from datetime import datetime
isinstance(pd.Timestamp(d), datetime)

But in case you really want a vanilla datetime, you can still do:

pd.Timestamp(d).to_datetime()

Timestamps are a lot more powerful than datetimes, amongst others when dealing with timezones. Actually, Timestamps are so powerful that it's a pity they are so poorly documented...

6
  • Good call. Came here as I thought it would need a python datetime solution to a pandas DatetimeIndex issue. Shame pandas doesn't have the concept of a DateIndex. Commented Nov 16, 2016 at 17:13
  • 46
    why do we require a heavyweight package just to do datetime manipulation, even in 2016?!
    – cowbert
    Commented Jul 24, 2018 at 0:50
  • 7
    Bringing out pandas for this is like taking a hammer to an ant
    – Dee S
    Commented May 26, 2020 at 2:35
  • 1
    using pandas to solve this problem is major overkill Commented Apr 20, 2021 at 11:25
  • 1
    to_datetime() has been deprecated, so use to_pydatetime() instead. This is a good answer for those that already have to use Pandas for reasons and need datetime format as well. Commented Aug 8, 2022 at 8:41
6

One way to convert from date to datetime that hasn't been mentioned yet:

from datetime import date, datetime
d = date.today()
datetime.strptime(d.strftime('%Y%m%d'), '%Y%m%d')
1
  • 3
    True, but converting ints to strings and back is probably rather more expensive than working directly with ints, so I'd only use this one for low-volume processing
    – Egor Kraev
    Commented Mar 9, 2018 at 13:02
4

Do I really have to manually call datetime(d.year, d.month, d.day)

No, you'd rather like to call

date_to_datetime(dt)

which you can implement once in some utils/time.py in your project:

from typing import Optional
from datetime import date, datetime

def date_to_datetime(
    dt: date,
    hour: Optional[int] = 0,
    minute: Optional[int] = 0, 
    second: Optional[int] = 0) -> datetime:

    return datetime(dt.year, dt.month, dt.day, hour, minute, second)
2
  • Probably an idea to add tz to that
    – JayTurnr
    Commented Feb 11, 2022 at 9:07
  • better, as this will be quicker than converting to string and back Commented Jul 14, 2022 at 4:12
4

An alternative to toisoformat/fromisoformat: you can use date.toordinal and datetime.fromordinal:

import datetime

start_date = datetime.date(1991, 2, 20)
start_date_midnight = datetime.datetime.fromordinal(start_date.toordinal())

I suspect this is more efficient than converting to/from a string.

You can test this process as so:

def test_datetime_from_date():
    for x in range(1,1000000):
        date_ = datetime.date.fromordinal(x)
        datetime_ = datetime.datetime.fromordinal(date_.toordinal())
        datetime_iso_date, t, datetime_iso_time = datetime_.isoformat().partition("T")
        assert datetime_iso_date == date_.isoformat()
3

You can use easy_date to make it easy:

import date_converter
my_datetime = date_converter.date_to_datetime(my_date)

Disclosure: I'm the owner of the package.

1
  • 1
    You should explicitly disclose that this is your own package.
    – Ry-
    Commented Mar 3 at 10:35
0

To make dt timezone aware datetime (with Django timezone util):

from django.utils import timezone


timezone.now().replace(*(*dt.timetuple()[:6], 0))
0

A rather simple way only using datetime:

from datetime import datetime

datetime.timestamp(datetime.combine(d, datetime.min.time()))

It converts the datetime.date to a datetime.datetime, and gives the timestamp of that.

Not the answer you're looking for? Browse other questions tagged or ask your own question.