📅  最后修改于: 2023-12-03 14:55:37.156000             🧑  作者: Mango
这个程序员工具旨在帮助你快速查询获取未来的最近日期。无论你是在开发日程管理软件、倒计时功能还是需要获取未来日期的任何其他场景,这个工具都能帮到你。
import datetime
def find_next_birthday(birthdate):
today = datetime.date.today()
next_birthday = datetime.date(today.year, birthdate.month, birthdate.day)
if next_birthday < today:
next_birthday = datetime.date(today.year+1, birthdate.month, birthdate.day)
days_until_birthday = (next_birthday - today).days
return next_birthday, days_until_birthday
# 使用示例
birthdate = datetime.date(1990, 5, 15)
next_birthday, days_until_birthday = find_next_birthday(birthdate)
print("下一个生日: ", next_birthday.strftime("%Y-%m-%d"))
print("距离下一个生日还有 {} 天".format(days_until_birthday))
import datetime
def find_next_holiday(holidays):
today = datetime.date.today()
next_holiday = min(holidays)
if next_holiday < today:
next_holiday = min(holiday for holiday in holidays if holiday > today)
days_until_holiday = (next_holiday - today).days
return next_holiday, days_until_holiday
# 使用示例
holidays = [datetime.date(2022, 1, 1), datetime.date(2022, 4, 4), datetime.date(2022, 5, 1)]
next_holiday, days_until_holiday = find_next_holiday(holidays)
print("下一个节假日: ", next_holiday.strftime("%Y-%m-%d"))
print("距离下一个节假日还有 {} 天".format(days_until_holiday))
import datetime
def find_next_weekend():
today = datetime.date.today()
next_weekend = today + datetime.timedelta(days=(5-today.weekday()+7)%7)
if next_weekend == today:
next_weekend += datetime.timedelta(days=7)
days_until_weekend = (next_weekend - today).days
return next_weekend, days_until_weekend
# 使用示例
next_weekend, days_until_weekend = find_next_weekend()
print("下一个周末: ", next_weekend.strftime("%Y-%m-%d"))
print("距离下一个周末还有 {} 天".format(days_until_weekend))
通过这个工具,你可以方便地查询到未来的最近日期,从而使你的代码更灵活、更个性化。无论你是需要计算剩余时间、制定提醒功能还是创建日程管理软件,这个工具都是一个有用的工具。
注意: 以上示例中的日期格式、日期计算方式等均为简化实现,实际应用中需要根据具体的需求进行改进和优化。