month name to month number and vice versa in python

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

    Building on ideas expressed above, This is effective for changing a month name to its appropriate month number:

    from time import strptime
    monthWord = 'september'
    
    newWord = monthWord [0].upper() + monthWord [1:3].lower() 
    # converted to "Sep"
    
    print(strptime(newWord,'%b').tm_mon) 
    # "Sep" converted to "9" by strptime
    

提交回复
热议问题