
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Convert date string to timestamp in Python
When it is required to convert a string into a timestamp, the ‘mktime’ method is used. This method is present in the ‘time’ package.
Below is a demonstration of the same −
Example
import time import datetime my_string = "24/03/2021" print("The date string is :") print(my_string) print("The timestamp is :") print(time.mktime(datetime.datetime.strptime(my_string, "%d/%m/%Y").timetuple()))
Output
The date string is : 24/03/2021 The timestamp is : 1616544000.0
Explanation
The required packages are imported.
The string is defined, and displayed on the console.
The ‘mktime’ method from time package is called, and the string is passed as parameter to it.
The ‘strptime’ is used to remove the extra spaces or symbols from the string.
The output is displayed on the console.
- Related Articles
- How to convert unix timestamp string to readable date in Python?
- How to convert Python date to Unix timestamp?
- How to convert date to timestamp in MongoDB
- How to convert timestamp string to datetime object in Python?
- Convert MySQL Unix-Timestamp format to date format?
- Convert DATE timestamp to return the month number
- Select timestamp as date string in MySQL?
- How to convert a Python date string to a date object?
- Convert string (varchar) to timestamp format in MySQL?
- How to convert a timestamp object in to Date in JDBC program?
- How to convert a Date object in to Timestamp in JDBC program?
- Convert DATE timestamp to return only the month name in MySQL
- Convert String to Date in Java
- Convert dd/mm/yyyy string to Unix timestamp in MySQL?
- Python Pandas - Convert PeriodIndex object to Timestamp

Advertisements