Easy Tutorial
❮ Python Module Python Att Time Mktime ❯

Python Convert String Time to Timestamp

Python3 Examples

Given a string time, convert it to a timestamp.

Example

import time

a1 = "2019-5-10 23:40:00"
# Convert to time array
timeArray = time.strptime(a1, "%Y-%m-%d %H:%M:%S")

# Convert to timestamp
timeStamp = int(time.mktime(timeArray))
print(timeStamp)

# Format conversion - change to /
a2 = "2019/5/10 23:40:00"
# Convert to time array, then to other format
timeArray = time.strptime(a2, "%Y/%m/%d %H:%M:%S")
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print(otherStyleTime)

Executing the above code outputs:

1557502800
2019/05/10 23:40:00

Python3 Examples

❮ Python Module Python Att Time Mktime ❯