Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert date to datetime in Python?
In this article, we will discuss how to convert a date to a datetime object in Python. We use the combine() method from the Date & Time module to combine a date object and a time object into a single datetime object.
While the date object represents only the calendar date (year, month, day), sometimes we need the full datetime object that includes time (hour, minute, second) as well. Following are several ways to achieve this ?
Syntax
The syntax of the combine() method is as follows ?
datetime.combine(date, time)
Using combine() with Minimum Time
The combine() method takes date and time as parameters. It creates a new datetime object by combining the provided date and time components.
If we have a date object and don't have a time object, we can initialize the time object to minimum using datetime.min.time() (midnight, i.e., 00:00:00) ?
from datetime import date, datetime
my_date = date.today()
print("The date object:", my_date)
my_time = datetime.min.time()
print("The time object:", my_time)
my_datetime = datetime.combine(my_date, my_time)
print("The combined datetime object:", my_datetime)
The date object: 2025-01-27 The time object: 00:00:00 The combined datetime object: 2025-01-27 00:00:00
Using the datetime() Constructor
The datetime() constructor creates a datetime object from specific year, month, and day values. You can convert a date object by passing the date's year, month, and day as arguments. The resulting datetime object will have the specified date and a time of midnight (00:00:00) ?
from datetime import date, datetime
my_date = date.today()
my_datetime = datetime(my_date.year, my_date.month, my_date.day)
print("Original date:", my_date)
print("Converted datetime:", my_datetime)
Original date: 2025-01-27 Converted datetime: 2025-01-27 00:00:00
Using combine() with Custom Time
In some cases, we may want to specify a custom time rather than using midnight (00:00:00). We can create a time object manually and combine it with our date ?
from datetime import date, time, datetime
my_date = date(2024, 12, 25)
print("The date object:", my_date)
my_time = time(14, 30, 45)
print("The time object:", my_time)
my_datetime = datetime.combine(my_date, my_time)
print("The combined datetime object:", my_datetime)
The date object: 2024-12-25 The time object: 14:30:45 The combined datetime object: 2024-12-25 14:30:45
Comparison
| Method | Use Case | Time Component |
|---|---|---|
combine() + min.time() |
Convert with midnight time | 00:00:00 |
datetime() constructor |
Direct conversion | 00:00:00 |
combine() + custom time |
Convert with specific time | User-defined |
Conclusion
Use datetime.combine() for flexible date-to-datetime conversion. The datetime() constructor is simpler for midnight conversions, while custom time objects allow specific time values.
