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

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

    For anyone trying to get the quarter of the fiscal year, which may differ from the calendar year, I wrote a Python module to do just this.

    Installation is simple. Just run:

    $ pip install fiscalyear
    

    There are no dependencies, and fiscalyear should work for both Python 2 and 3.

    It's basically a wrapper around the built-in datetime module, so any datetime commands you are already familiar with will work. Here's a demo:

    >>> from fiscalyear import *
    >>> a = FiscalDate.today()
    >>> a
    FiscalDate(2017, 5, 6)
    >>> a.fiscal_year
    2017
    >>> a.quarter
    3
    >>> b = FiscalYear(2017)
    >>> b.start
    FiscalDateTime(2016, 10, 1, 0, 0)
    >>> b.end
    FiscalDateTime(2017, 9, 30, 23, 59, 59)
    >>> b.q3
    FiscalQuarter(2017, 3)
    >>> b.q3.start
    FiscalDateTime(2017, 4, 1, 0, 0)
    >>> b.q3.end
    FiscalDateTime(2017, 6, 30, 23, 59, 59)
    

    fiscalyear is hosted on GitHub and PyPI. Documentation can be found at Read the Docs. If you're looking for any features that it doesn't currently have, let me know!

提交回复
热议问题