Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Convert a String Time to a Timestamp

Python Convert a String Time to a Timestamp

Document Object Reference Manual Python3 Examples

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

)

)

print

(

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

)

print

(

otherStyleTime

)

The output of the code above is:


1557502800
2019/05/10 23:40:00

Document Object Reference Manual Python3 Examples