Convert a date string into YYYYMMDD

前端 未结 3 2282
北荒
北荒 2021-02-14 22:42

I\'ve got a bunch of date strings in this form: -

30th November 2009
31st March 2010
30th September 2010

I want them like this: -



        
3条回答
  •  别跟我提以往
    2021-02-14 22:50

    Try dateutil:

    from dateutil import parser
    
    dates = ['30th November 2009', '31st March 2010', '30th September 2010']
    
    for date in dates:
        print parser.parse(date).strftime('%Y%m%d')
    

    output:

    20091130
    20100331
    20100930
    

    or if you want to do it using standard datetime module:

    from datetime import datetime
    
    dates = ['30th November 2009', '31st March 2010', '30th September 2010']
    
    for date in dates:
        part = date.split()
        print datetime.strptime('%s %s %s' % (part[0][:-2]), part[1], part[2]), '%d %B %Y').strftime('%Y%m%d')
    

提交回复
热议问题