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

后端 未结 14 1676
梦如初夏
梦如初夏 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:34

    I would suggest another arguably cleaner solution. If X is a datetime.datetime.now() instance, then the quarter is:

    import math
    Q=math.ceil(X.month/3.)
    

    ceil has to be imported from math module as it can't be accessed directly.

提交回复
热议问题