Is there a Python function to determine which quarter of the year a date is in?

后端 未结 14 1703
梦如初夏
梦如初夏 2020-11-29 00:07

Sure I could write this myself, but before I go reinventing the wheel is there a function that already does this?

14条回答
  •  余生分开走
    2020-11-29 00:52

    using dictionaries, you can pull this off by

    def get_quarter(month):
        quarter_dictionary = {
            "Q1" : [1,2,3],
            "Q2" : [4,5,6],
            "Q3" : [7,8,9],
            "Q4" : [10,11,12]
        }
    
        for key,values in quarter_dictionary.items():
            for value in values:
                if value == month:
                    return key
    
    print(get_quarter(3))
    

提交回复
热议问题