Computer >> Computer tutorials >  >> Programming >> MySQL

How to convert Python date format to 10-digit date format for mysql?


If you have the date in the form of a datetime object, you can simply get the timetuple for that object and pass it to the time.mktime function. 

Example

import time
from datetime import date
from datetime import datetime
my_date = date.today()
print(time.mktime(my_date.timetuple()))

Output

This will give the output −

1514917800.0

If its in form of a string, you can use the strptime function to parse the string to a time object and then use the time.mktime() function to create the mysql supported date format. 

Example

import time
my_time = time.strptime("01-01-18", "%d-%m-%y")
print(time.mktime(my_time))

Output

This will give the output −

1514745000.0