简体   繁体   中英

how to parse a string date to a proper date format?

I am using a python "datefinder" library to obtain a "datetime" object. However, i encountered some issues where i have a string: "the 6m day of June 2012" and the "datefinder" library could not generate the correct date of that string.

I have tried using

datefinder.find_dates(date)
Result: 2012-06-13 00:00:00

The above result certainly is wrong and i realized the culprit is the "6m" word.

How do i convert the string "the 6m day of June 2012" to a proper format like "6 June 2012"?

Try this one, It gives the o/p you want :

import datetime

p = datetime.datetime.strptime('the 6m day of June 2012','the %dm day of %B %Y')
print(p)

O/p is like :

2012-06-06 00:00:00

If you want it in desired format 06 June 2012 you can use this :

print(p.strftime("%d %B %Y"))

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