python3.10 提供一个起止年月作为参数,自动生成每月起止年月日的龛套 list 列表
时间: 2024-05-02 17:22:50 浏览: 244
好的,我了解您的问题。您可以尝试使用 Python 自带的 datetime 和 calendar 模块来生成每个月的起止年月日。以下是代码示例:
```
import datetime
import calendar
def generate_dates(start_year, start_month, end_year, end_month):
start_date = datetime.date(start_year, start_month, 1)
end_date = datetime.date(end_year, end_month, 1)
end_date = end_date.replace(day=calendar.monthrange(end_date.year, end_date.month)[1])
dates = []
curr_date = start_date
while curr_date <= end_date:
dates.append([curr_date.strftime('%Y-%m-%d'), curr_date.replace(day=calendar.monthrange(curr_date.year, curr_date.month)[1]).strftime('%Y-%m-%d')])
curr_date = curr_date.replace(day=1) + datetime.timedelta(days=calendar.monthrange(curr_date.year, curr_date.month)[1])
return dates
# 起止年月
start_year = 2022
start_month = 1
end_year = 2022
end_month = 12
# 生成日期列表
dates = generate_dates(start_year, start_month, end_year, end_month)
# 打印日期列表
print(dates)
```
在上述代码中,我们定义了一个 `generate_dates` 函数来生成每个月的起止年月日。该函数接受起止年月作为参数,并利用循环来生成每个月的起止日期,并将其存储在一个列表中返回。
最后,我们调用 `generate_dates` 函数,并将起止年月作为参数传入。该函数将返回一个包含每个月起止日期的列表,并将其打印出来。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)