封装一个函数,输入当年放假的日期信息,输出当天距离放假的天数和已放假过的天数(假期取春节,清明节,五一,端午节,中秋节,国庆节,元旦)
时间: 2024-04-15 11:32:08 浏览: 81
好的,我明白了。下面是一个封装函数的示例,用于计算距离放假的天数和已放假过的天数:
```python
from datetime import datetime
def calculate_holiday_days(holiday_date):
holidays = {
"春节": "2022-02-01", # 请根据实际情况修改日期
"清明节": "2022-04-04",
"五一": "2022-05-01",
"端午节": "2022-06-03",
"中秋节": "2022-09-10",
"国庆节": "2022-10-01",
"元旦": "2023-01-01"
}
today = datetime.now().date()
holiday_date = datetime.strptime(holiday_date, "%Y-%m-%d").date()
if holiday_date < today:
days_until_holiday = -1
days_holiday_passed = (today - holiday_date).days
elif holiday_date == today:
days_until_holiday = 0
days_holiday_passed = 0
else:
days_until_holiday = (holiday_date - today).days
days_holiday_passed = 0
return days_until_holiday, days_holiday_passed
# 示例用法
holiday = input("请输入放假日期(格式:YYYY-MM-DD):")
days_until, days_passed = calculate_holiday_days(holiday)
print("距离放假还有", days_until, "天")
print("已经放假了", days_passed, "天")
```
你可以根据实际情况修改 holidays 字典中的日期。使用该函数,你只需要提供放假的日期,它就会计算出距离放假的天数和已放假过的天数。
阅读全文