在 Python 中获取当前季度的第一个日期和最后一个日期?
问题描述:
如何在 Python 中获取当前季度年份,然后是当前季度年份的第一个日期和最后一个日期?
How can i get the Current Quarter year and then First Date and last Date of Current Quarter Year in Python?
我想通过导入日期时间
import datetime
人们研究堆栈溢出需要直接的答案,这应该非常简单.您提供的哪个链接有很多评论.因此,用户必须浏览所有评论才能找到正确答案.我正在写简单明了的答案.
People look into Stack overflow need straight forward answer and which should be very simple. Which ever link you provided it having lot of Comments. SO, users has to go through all the comments to find correct answer. I am writing simple and straight forward answer.
答
我如何在 c# 中找到一些简单的解决方案并将其转换为 python,
Any how i found some simple solution in c# and converted it into python,
from datetime import datetime,timedelta
current_date=datetime.now()
currQuarter = (current_date.month - 1) / 3 + 1
dtFirstDay = datetime(current_date.year, 3 * currQuarter - 2, 1)
dtLastDay = datetime(current_date.year, 3 * currQuarter + 1, 1) + timedelta(days=-1)