My python code retrieves a datetime column from a mySql server database. I can print the datetime as it is stored on the server (ex. 2018-10-22, however I need to print it in a more readable way like 22-10-2018. Basically, row[0] holds datetime data as the code below. Thanks for any help.
JavaScript
x
12
12
1
if mycursor.rowcount > 0:
2
3
print("Total rows are: ", len(myresult))
4
for row in myresult:
5
print("Record Number: " + str(countLines))
6
print("-----------------")
7
print("Time: ", row[0])
8
print("Latitude: ", row[1])
9
print("Longitude: ", row[2])
10
print("Accuracy: ", row[3])
11
print("Place: ", row[4])
12
Advertisement
Answer
If your time
is a datetime
object you can use strftime()
JavaScript
1
4
1
from datetime import datetime
2
3
row[0].strftime('%d-%m-%Y')
4
Output
JavaScript
1
5
1
t = datetime(2018, 10, 22)
2
t.strftime('%d-%m-%Y')
3
4
#'22-10-2018'
5
If your data is a string
you will need to define it as a datetime
object first.