Convert a date string into YYYYMMDD

前端 未结 3 1346
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 22:06

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:46

    You can almost do this with a combination of strptime and strptime from the datetime module.

    The problem we have is that the built-in formats support dates like 30 November 2010 but not 30th November 2010. So in the example below I've used a regular expression substitution to strip out the problem characters. (The regular expression uses a look-behind to see if "st", "nd", "rd" or "th" is preceeded by a digit, and if so replaces it with the empty string, thus removing it from the string.)

    >>> import re
    >>> from datetime import datetime
    >>> mydate = "30th November 2009"
    >>> mydate = re.sub("(?<=\d)(st|nd|rd|th)","",mydate)
    >>> mydate
    '30 November 2009'
    >>> mydatetime = datetime.strptime(mydate,"%d %B %Y")
    >>> mydatetime
    datetime.datetime(2009, 11, 30, 0, 0)
    >>> mydatetime.strftime("%Y%M%d")
    '20090030'
    
    0 讨论(0)
  • 2021-02-14 22:57

    In python 3.7, you can use isoformat()

    >>> from datetime import datetime
    >>> datetime.today().date().isoformat().replace("-", "")
    '20190220'
    
    0 讨论(0)
  • 2021-02-14 23:05

    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')
    
    0 讨论(0)
提交回复
热议问题