Python ProgrammingPython Programming

Get First Date and Last Date of Current Quarter

##
# Python's program to get first and last day of Current Quarter Year

from datetime import datetime, timedelta

current_date = datetime.now()
current_quarter = round((current_date.month - 1) / 3 + 1)
first_date = datetime(current_date.year, 3 * current_quarter - 2, 1)
last_date = datetime(current_date.year, 3 * current_quarter + 1, 1)\
    + timedelta(days=-1)

print("First Day of Quarter:", first_date)
print("Last Day of Quarter:", last_date)

Sample output of above program.
C:\programs\time>pep8 --first example20.py

C:\programs\time>python example20.py
First Day of Quarter: 2018-01-01 00:00:00
Last Day of Quarter: 2018-03-31 00:00:00

C:\programs\time>