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

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

    This is very simple and works in python3:

    from datetime import datetime
    
    # Get current date-time.
    now = datetime.now()
    
    # Determine which quarter of the year is now. Returns q1, q2, q3 or q4.
    quarter_of_the_year = 'q'+str((now.month-1)//3+1)
    

提交回复
热议问题