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 Program to Convert datetime to string
from datetime import datetime # current date and time now = datetime.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
- First we import datetime class from datetime module.
- Get the current date and time
- Convert datetime to string using strftime method.
- Then we get the output as string.