简体   繁体   中英

string convert to date time in python

I have a list of date time strings like this.

16-Aug-2019

I want to convert the string to 2019-08-01 this date format, and I have tried on this code , but it's getting me an error.

formatd_date = datetime.strptime(formatd_date, '%y-%m-%d')

ValueError: time data 'As-of' does not match format '%y-%m-%d'

If any can help, it will be huge thank.

Convert to datetime format and then convert to string format you want to:

>>> from datetime import datetime
>>> a = "16-Aug-2019"
>>> datetime.strptime(a, "%d-%b-%Y").strftime("%Y-%m-%d")
'2019-08-16'

Documentation: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

Just fails because %y is 2-digit year. Use %Y for 4-digit year.

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