9951 explained code solutions for 126 technologies


python-mysqlHow can I use Python to convert a MySQL datetime value to a different format?


Python can be used to convert a MySQL datetime value to a different format by using the datetime module and the strftime() function. The following example code illustrates this:

# import datetime module
import datetime

# define the datetime value
datetime_value = '2020-08-01 12:30:00'

# convert the string into a datetime object
datetime_object = datetime.datetime.strptime(datetime_value, '%Y-%m-%d %H:%M:%S')

# convert the datetime object into a different format
new_datetime_format = datetime_object.strftime('%B %d, %Y %H:%M')

# print the new datetime format
print(new_datetime_format)

Output example

August 01, 2020 12:30

Code explanation

  1. import datetime: imports the datetime module which contains the necessary functions for manipulating date and time values.
  2. datetime_value = '2020-08-01 12:30:00': defines the datetime value as a string.
  3. datetime_object = datetime.datetime.strptime(datetime_value, '%Y-%m-%d %H:%M:%S'): converts the string into a datetime object using the strptime() function.
  4. new_datetime_format = datetime_object.strftime('%B %d, %Y %H:%M'): converts the datetime object into a different format using the strftime() function.
  5. print(new_datetime_format): prints the new datetime format.

Helpful links

  1. Python datetime module
  2. Python strftime() function

Edit this code on GitHub