Convert date string to timestamp Using timestamp()
Approach:
- import datetime is use to import the date and time modules
- After importing date time module next step is to accept date string as an input and then store it into a variable
- Then we use strptime method. This method takes two arguments
- First arguments s the string format of the date and time
- Second argument is the format of the input string
- Then it returns date and time value in timestamp format and we stored this in timestamp variable.
Example 1:
Python3
# Python program to convert # date to timestamp import time import datetime string = "20/01/2020" element = datetime.datetime.strptime(string, "%d/%m/%Y" ) timestamp = datetime.datetime.timestamp(element) print (timestamp) |
Output:
1579458600.0
Convert date string to timestamp in Python
The Most Common way we use to store dates and times into a Database is in the form of a timestamp.
date and time in form of a string before storing it into a database, we convert that date and time string into a timestamp. Python provides various ways of converting the date to timestamp. Few of them discussed in this article are:
- Using timetuple()
- Using timestamp()
Contact Us