Convert Date to Datetime in Python

Convert Date to Datetime in Python

Abdul Jabbar Mar-29, 2022 Oct-06, 2021 Python Python DateTime
  1. Convert Date to DateTime Using the datetime Combine Method in Python
  2. Convert Date to DateTime Using the datetime Object Method in Python

In Python, date and time can be followed through its built-in libraries. In this precious article, we will learn how to convert date to datetime in Python.

We will be dealing with date and time in Python at the same time. And that can be a big struggle for you. There is always a way better for everything if we want to find it. Fortunately, there’s a built-in way of making it effortless: the Python datetime module.

This article will show how we can import the data and datetime from the built-in datetime python module. Then we will use some Python built-in functions to combine both date and time into a single object. If you have only any specific date or the current date and don’t have a specific time, you can initialize it with the minimum time using the datetime object. We will see these examples below in the code.

We will also look at examples of how we can convert the date into datetime using various methods.

Convert Date to DateTime Using the datetime Combine Method in Python

In this method, we will first import the date and datetime from the datetime built-in object, then we will extract the current date and minimum time respectively. Both objects will be merged using Python datetime combine built-in method.

Example Codes:

# python 3.x
from datetime import date as todaysDate
from datetime import datetime as todaysDateTime
Today_date  =  todaysDate.today()
Today_time  =  todaysDateTime.min.time()
Today_datetime = todaysDateTime.combine(Today_date, Today_time)
print(Today_datetime)

Output:

2021-10-03 00:00:00

Convert Date to DateTime Using the datetime Object Method in Python

In this technique, we will first import the date and datetime from the datetime built-in object, then we will extract todays date. Furthermore, the current year, month, and day will be passed to the current datetime object to get the result object having date and time together.

Example Codes:

# python 3.x
from datetime import date as todaysDate
from datetime import datetime as todaysDateTime
Todays_date = todaysDate.today()
Todays_datetime = todaysDateTime(Todays_date.year, Todays_date.month, Todays_date.day)
print(Todays_datetime)

Output:

2021-10-03 00:00:00
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Python DateTime

  • Convert Pandas Column to Datetime
  • Get the Current Time in Python
  • Get the Day of the Week in Python
  • Convert String to Datetime in Python
  • Find the Last Day of the Month Using Python
  • Determine Leap Year in Python