python 获取当前季度的开始时间和结束时间
时间: 2023-06-28 14:09:24 浏览: 60
可以使用 Python 中的 datetime 模块来获取当前季度的开始时间和结束时间。以下是一个示例代码:
```python
import datetime
today = datetime.date.today()
year = today.year
month = today.month
quarter_start_month = 3 * ((month - 1) // 3) + 1 # 当前季度的开始月份
quarter_start_date = datetime.date(year, quarter_start_month, 1) # 当前季度的开始日期
quarter_end_date = quarter_start_date.replace(month=quarter_start_month+2, day=31) # 当前季度的结束日期
# 处理当前季度结束日期的月份不为当前季度最后一个月的情况
if quarter_end_date.month != quarter_start_month+2:
quarter_end_date = quarter_end_date.replace(day=quarter_end_date.day-1)
print("当前季度的开始日期为:", quarter_start_date)
print("当前季度的结束日期为:", quarter_end_date)
```
这个代码会输出当前季度的开始日期和结束日期,例如:
```
当前季度的开始日期为: 2021-07-01
当前季度的结束日期为: 2021-09-30
```