Python Convert String Time to Timestamp

Document 对象参考手册Python3 Examples

Given a string time, convert it to a timestamp.

Example

import time a1 = "2019-5-10 23:40:00" # First 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 - convert to / a2 = "2019/5/10 23:40:00" # First convert to time array, then convert to other formats 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 produces the output:

1557502800
2019/05/10 23:40:00

Document 对象参考手册Python3 Examples

Other Extensions