Convert datetime to string in Python

In this article, we are going to learn how we can convert from date time to a string in Python. We will make use of the datetime module of Python to get the current date and time. Once we have the current date and time then we will convert it to a string in format MMDDYY HHMMSS.

Environment and requirements-
Python interpreter
datetime library

Python Program to Convert datetime to string

from datetime import datetime
# current date and time
now = 
date_time = now.strftime("%m/%d/%Y %H:%M:%S")
print("The Converted date and time in String is:",date_time)


The Converted date and time in String is: 01/03/2023 13:54:27

Code explanation-

  1. First we import datetime class from datetime module.
  2. Get the current date and time
  3. Convert datetime to string using strftime method.
  4. Then we get the output as string.