Skip to main content

How to Convert Date to Datetime in Python

A date object contains only year, month, and day information. Many databases, APIs, and timestamp systems require datetime objects that include time components (hour, minute, second).

Since a date has no inherent time, you must specify one, typically midnight (00:00:00) unless your use case requires something different.

The cleanest approach combines your date with a time object using datetime.combine():

from datetime import date, datetime, time

d = date(2026, 12, 25)

# Combine date with midnight (00:00:00)
dt = datetime.combine(d, time.min)

print(dt) # 2026-12-25 00:00:00
print(type(dt)) # <class 'datetime.datetime'>

Output:

2026-12-25 00:00:00
<class 'datetime.datetime'>
Alternative Midnight Syntax

Both time.min and time() represent midnight. Choose whichever reads more clearly in your context:

dt = datetime.combine(d, time.min)  # Explicit minimum time
dt = datetime.combine(d, time()) # Default time constructor

Setting a Specific Time

When you need a particular time rather than midnight:

from datetime import date, datetime, time

d = date(2026, 12, 25)

# End of business day (5:30 PM)
end_of_day = datetime.combine(d, time(17, 30))
print(end_of_day) # 2026-12-25 17:30:00

# Last moment of the day (23:59:59.999999)
day_end = datetime.combine(d, time.max)
print(day_end) # 2026-12-25 23:59:59.999999

# Start of business (9:00 AM)
business_start = datetime.combine(d, time(9, 0, 0))
print(business_start) # 2026-12-25 09:00:00

Output:

2026-12-25 17:30:00
2026-12-25 23:59:59.999999
2026-12-25 09:00:00

Manual Construction

You can also construct a datetime directly from date components:

from datetime import date, datetime

d = date(2026, 12, 25)

# Extract components and build datetime
dt = datetime(d.year, d.month, d.day)
print(dt) # 2026-12-25 00:00:00

# With specific time
dt = datetime(d.year, d.month, d.day, 14, 30, 0)
print(dt) # 2026-12-25 14:30:00

Output:

2026-12-25 00:00:00
2026-12-25 14:30:00
note

While this works, datetime.combine() is more readable and explicitly shows the intent to merge a date with a time.

Adding Timezone Information

For timezone-aware datetimes:

from datetime import date, datetime, time, timezone, timedelta

d = date(2026, 12, 25)

# UTC timezone
dt_utc = datetime.combine(d, time.min, tzinfo=timezone.utc)
print(dt_utc) # 2026-12-25 00:00:00+00:00

# Custom timezone (e.g., UTC+5:30)
ist = timezone(timedelta(hours=5, minutes=30))
dt_ist = datetime.combine(d, time.min, tzinfo=ist)
print(dt_ist) # 2026-12-25 00:00:00+05:30

Output:

2026-12-25 00:00:00+00:00
2026-12-25 00:00:00+05:30

Using zoneinfo (Python 3.9+)

from datetime import date, datetime, time
from zoneinfo import ZoneInfo

d = date(2026, 12, 25)

# Named timezone
dt_ny = datetime.combine(d, time.min, tzinfo=ZoneInfo("America/New_York"))
print(dt_ny) # 2026-12-25 00:00:00-05:00

dt_tokyo = datetime.combine(d, time.min, tzinfo=ZoneInfo("Asia/Tokyo"))
print(dt_tokyo) # 2026-12-25 00:00:00+09:00

Output:

2026-12-25 00:00:00-05:00
2026-12-25 00:00:00+09:00

Converting from String Input

If your date arrives as a string, parse directly to datetime:

from datetime import datetime

date_str = "2026-12-25"

# Parse string directly to datetime (includes midnight by default)
dt = datetime.strptime(date_str, "%Y-%m-%d")
print(dt) # 2026-12-25 00:00:00

# For ISO format strings
dt = datetime.fromisoformat("2026-12-25")
print(dt) # 2026-12-25 00:00:00

Output:

2026-12-25 00:00:00
2026-12-25 00:00:00

Working with Today's Date

from datetime import date, datetime, time

# Today at midnight
today_midnight = datetime.combine(date.today(), time.min)
print(today_midnight)

# Today at current time (simpler alternative)
now = datetime.now()
print(now)

# Today at specific time
today_noon = datetime.combine(date.today(), time(12, 0))
print(today_noon)

Output:

2026-02-13 00:00:00
2026-02-13 10:24:50.622366
2026-02-13 12:00:00

Common Pitfalls

Immutability

Date and datetime objects are immutable. You cannot modify them in place:

from datetime import date, datetime

d = date.today()

# ⛔️ This fails - dates have no time attributes
# d.hour = 0 # AttributeError

# ⛔️ This also fails - datetimes are immutable
dt = datetime.now()
# dt.hour = 5 # AttributeError

# ✅ Create a new object instead
dt = datetime.combine(d, time(5, 0))

Confusing date and datetime

from datetime import date, datetime

# These are different types
d = date(2026, 12, 25)
dt = datetime(2026, 12, 25)

print(type(d)) # <class 'datetime.date'>
print(type(dt)) # <class 'datetime.datetime'>

# datetime has time attributes, date does not
print(dt.hour) # 0
print(d.hour) # AttributeError: 'datetime.date' object has no attribute 'hour'

Practical Example: Database Timestamps

from datetime import date, datetime, time, timezone

def date_to_db_timestamp(d: date, end_of_day: bool = False) -> datetime:
"""Convert date to timezone-aware datetime for database storage."""
if end_of_day:
t = time(23, 59, 59, 999999)
else:
t = time.min

return datetime.combine(d, t, tzinfo=timezone.utc)

# Usage
start_date = date(2026, 1, 1)
end_date = date(2026, 12, 31)

# Query: Find records between start and end of date range
query_start = date_to_db_timestamp(start_date)
query_end = date_to_db_timestamp(end_date, end_of_day=True)

print(f"From: {query_start}") # 2026-01-01 00:00:00+00:00
print(f"To: {query_end}") # 2026-12-31 23:59:59.999999+00:00

Output:

From: 2026-01-01 00:00:00+00:00
To: 2026-12-31 23:59:59.999999+00:00

Summary

GoalCode
Date to midnightdatetime.combine(d, time.min)
Date to specific timedatetime.combine(d, time(17, 30))
Date to end of daydatetime.combine(d, time.max)
With timezonedatetime.combine(d, time.min, tzinfo=tz)
Manual constructiondatetime(d.year, d.month, d.day)
Best Practice

Use datetime.combine() for converting dates to datetimes. It's self-documenting, explicitly shows the intent to merge a calendar date with a clock time, and handles edge cases correctly.