
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 the several ways to achieve this.
The syntax of the combine() method is as follows.
datetime.combine(date,time)
Converting a date object to a datetime Object Using the combine() Method
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, then you initialize the time object to a minimum using the datetime object (Minimum means at midnight i.e., 00:00:00).
Example
In the following program, we combine a date object retrieved by using the today() method, and we initialise the time object to the minimum time (00:00:00) using the min.time() method. And combined these two objects by applying the datetime.combine() method.
from datetime import date from datetime import 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 output of the above code is as follows.
The date object: 2022-05-19 The date object: 2025-05-12 The time object: 00:00:00 The combined datetime object: 2025-05-12 00:00:00
Using the datetime() Constructor
The datetime() constructor creates a datetime object from specific year, month, and day values. You can use it to convert a date object into a datetime 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).
Example
In the following program, we pass the year, month and day values of the desired date object (as my_date.year, my_date.month, my_date.day) to this constructor to convert a date object to a datetime object.
from datetime import date from datetime import datetime my_date = date.today() my_datetime = datetime(my_date.year, my_date.month, my_date.day) print(my_datetime)
2022-09-05 00:00:00
Using datetime.combine() with Custom Time
In some cases, we may want to specify a custom time rather than using midnight (00:00:00). Then, we can create a time object (manually) and combine it with our date.
Example
In this program, we manually specify a date and a time object. The time object is created with specific hour and minute values, and then both are combined into a datetime object.
from datetime import date, time, datetime my_date = date(2012, 2, 12) print("The date object:", my_date) my_time = time(1, 30) print("The time object:", my_time) my_datetime = datetime.combine(my_date, my_time) print("The combined datetime object:", my_datetime)
Following is the output of the above code ?
The date object: 2012-02-12 The time object: 01:30:00 The combined datetime object: 2012-02-12 01:30:00