himabindu
himabindu

Reputation: 336

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"

Upvotes: 0

Views: 168

Answers (2)

jia Jimmy
jia Jimmy

Reputation: 1848

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



Upvotes: 1

Hasitha Amarathunga
Hasitha Amarathunga

Reputation: 2003

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)

Upvotes: 0

Related Questions