Python Convert a String Time to a Timestamp
Given a time as a string, convert it to a timestamp.
Example
import
time
a1
=
"
2019-5-10 23:40:00
"
First convert to a time array
timeArray
=
time
.
strptime
(
a1
,
"
%Y-%m-%d %H:%M:%S
"
)
Convert to a timestamp
timeStamp
=
int
(
time
.
mktime
(
timeArray
)
)
(
timeStamp
)
Format conversion - convert to /
a2
=
"
2019/5/10 23:40:00
"
First convert to a time array, then convert to another format
timeArray
=
time
.
strptime
(
a2
,
"
%Y/%m/%d %H:%M:%S
"
)
otherStyleTime
=
time
.
strftime
(
"
%Y/%m/%d %H:%M:%S
"
,
timeArray
)
(
otherStyleTime
)
The output of the code above is:
1557502800
2019/05/10 23:40:00
Python3 Examples