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

MySQLPythonProgrammingServer Side Programming

In this article, we will convert a python date format to 10-digit format for MySQL. We use the mktime() method from the time module provided by python.

The mktime() method

The python time method mktime() is the inverse function of the localtime().

The time.mktime() method of the Time module is used to convert a time.struct_time object or a tuple of 9 elements(which represents the time.struct_time object) to time in seconds since the epoch of the local system.


The syntax of mktime() method is as follows.


Where, t is a time.struct_time object or a tuple containing 9 elements corresponding to the time.struct_time object.

If the input value does not represent a valid time, either Overflow Error or Value Error will be raised.

If the date is in the form of a datetime object

If the date is in the form of a datetime object, then you can directly get the time tuple for that object and pass it to the time.mktime() function.

Example 1

In this example, we will check if the date given is in the form of the datetime object.

import time from datetime import date from datetime import datetime my_date = my_date = my_date.timetuple() print("Time tuple:",my_date) print(time.mktime(my_date))


On executing the above program, the following output is obtained.

Time tuple: time.struct_time(tm_year=2022, tm_mon=5, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=139, tm_isdst=-1)

Example 2

Let us look at another example of this −

import datetime import time my_date = datetime.datetime(2020, 5, 17) print(time.mktime(my_date.timetuple()))


This will give the output −


If the date is in the form of a string

Here if the date is in the form of a string, we can parse the string using the strptime() method. And then we use the time.mktime() function to create a MySQL supported date format.


In the following example code, we use the strptime() method to get the time object and time.mktime() method to convert in MySQL date format.

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


The output is as follows.


Using the strftime()

The strftime() method in python accepts a format string and converts the contents of the current date-time object accordingly, and returns the result in the form of a string.

If you pass '%s' as the format string, this method converts the date-time value into milliseconds and returns the result as a 10-digit value.


Let us look into an example of this −

import datetime date_time = datetime.datetime(2021, 10, 17) digitVal = date_time.strftime('%s') print(digitVal)



Using the timestamp() method

The timestamp() method of python returns the time value since the epoch time (1st January 1970) in seconds.

In the following example we are converting the date “17-10-2021” in to 10 digit value using the timestamp() method −


import datetime date_time = datetime.datetime(2021, 10, 17) digitVal = date_time.timestamp() print(digitVal)
Updated on 08-Sep-2022 06:59:26