month name to month number and vice versa in python

后端 未结 12 1200
情书的邮戳
情书的邮戳 2020-11-29 20:13

I am trying to create a function that can convert a month number to an abbreviated month name or an abbreviated month name to a month number. I thought this might be a commo

12条回答
  •  囚心锁ツ
    2020-11-29 20:53

    Information source: Python Docs

    To get month number from month name use datetime module

    import datetime
    month_number = datetime.datetime.strptime(month_name, '%b').month
    
    # To  get month name
    In [2]: datetime.datetime.strftime(datetime.datetime.now(), '%a %b %d, %Y')
    Out [2]: 'Thu Aug 10, 2017'
    
    # To get just the month name, %b gives abbrevated form, %B gives full month name
    # %b => Jan
    # %B => January
    dateteime.datetime.strftime(datetime_object, '%b')
    

提交回复
热议问题