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.

Syntax

The syntax of mktime() method is as follows.

Time.mktime(t)

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 = date.today() my_date = my_date.timetuple() print("Time tuple:",my_date) print(time.mktime(my_date))

Output

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)
1652898600.0

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()))

Output

This will give the output −

1589673600.0

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.

Example

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))

Output

The output is as follows.

1514745000.0

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.

Example

Let us look into an example of this −

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

Output

1634428800

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 −

Example

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

Advertisements