Convert Date Time to String with Milliseconds

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

Environment and Requirement: –
Python Interpreter
datetime Module

Convert Date time to String in Python

      from datetime import datetime
      my_datetime =
      format='%d/%m/%y %H:%M:%S:%MS'
      my_datetime = my_datetime.strftime(format)


28/12/22 07:57:43:57S

Code Explanation:-

  1. Import datetime from datetime module.
  2. Used “my_datetime” as an Object of datetime Class in which
    is stored and it will return current local date.
  3. Get input from user in specified format.
  4. my_datetime.strftime() is stored in “my_datetime” which will return the string representation of date.
  5. Finally, used print function to get output of code.