简体   繁体   中英

How to convert date(different formats) to epoch format at a time

I have the dates of different formats I just want to convert them all to epoch format at a time.

The date formats are like any of below formats :

1. "October 1, 2018"
2. "23/05/2018"
3. "20-02-2017"
4. "May 2016"

Recommend package python-dateutil python-dateutil

pip install python-dateutil

s1 = "October 1, 2018"
s2 = "23/05/2018"
s3 = "20-02-2017"
s4 = "May 2016"

from dateutil import parser
r1 = parser.parse(s1)
r2 = parser.parse(s2)
r3 = parser.parse(s3)
r4 = parser.parse(s4) # parser.parse(s4).timestamp() you can get timestamp this way
print(r1, r2, r3, r4)

# 2018-10-01 00:00:00
# 2018-05-23 00:00:00
# 2017-02-20 00:00:00
# 2016-05-04 00:00:00



Did you mean that? This is a standard way to convert String date to epoch DateTime

from datetime import datetime
dt = datetime.strptime('October 1 2018', '%b %d %Y')
print (dt)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM