month name to month number and vice versa in python

后端 未结 12 1199
情书的邮戳
情书的邮戳 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

    def month_num2abbr(month):
        month = int(month)
        import calendar
        months_abbr = {month: index for index, month in enumerate(calendar.month_abbr) if month}
        for abbr, month_num in months_abbr.items():
            if month_num==month:
                return abbr
        return False
    
    print(month_num2abbr(7))
    

提交回复
热议问题