
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to convert Python date format to 10-digit date format for mysql?
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)
- Related Articles
- Convert MySQL Unix-Timestamp format to date format?
- How to convert US date format to MySQL format in INSERT query?
- How to convert a date format in MySQL?
- How to correctly convert a date format into a MySQL date?
- Convert VARCHAR data to MySQL date format?
- How to convert Python date in JSON format?
- Convert VARCHAR Date to a different format in MySQL?
- Format MySQL date and convert to year-month-day
- MySQL date format to convert dd.mm.yy to YYYY-MM-DD?
- How to convert milliseconds to date format in Android?
- Date format to convert dates like Jan 2017, May 2018 to complete date in MySQL
- How to update the date format in MySQL?
- How to Convert yyyymmdd to Normal Date Format in Excel?
- Set format specifier in MySQL STR_TO_DATE() and convert string to date
- Convert Date not in a proper format with MySQL?
