python - 用于在字典中给定日期前 2 周发送提醒电子邮件的 Python 程序
问题描述
我有一个名为“结果”的列表,其中包含 50 个患者字典及其信息,包括他们的预约日期。我正在尝试创建一个提醒系统,在患者预约前 2 周和 2 天发送电子邮件。我在创建将今天的日期与约会日期进行比较并提取距今天日期 2 周和 2 天的约会的程序时遇到问题。
这是我到目前为止的脚本,但不知道如何包含 2 周和 2 天的条件
import datetime
now = datetime.datetime.now()
current_date= (now.strftime("%m/%d/%Y"))
current_time= (now.strftime("%I:%M %p"))
for i in results:
if i['Appointment Date'] >= current_date:
print("Upcoming Appointment")
else:
print("Passed Appointment")
解决方案
您需要转换i['Appointment Date']
为datetime
对象,然后用今天的日期检查它。
import datetime
now = datetime.datetime.now()
for i in results:
# convert to datetime, assuming the string format is "YYYY/MM/DD"
target_date = datetime.datetime.strptime(i['Appointment Date'],"%Y/%m/%d")
diff = target_date - now
if diff.days==14 or diff.days==2: # check for 2 days or 14 days
print("Upcoming Appointment")
else:
print("Passed Appointment")
推荐阅读
- java - 错误“kafkaconsumer 类型不是通用的,它不能用参数进行参数化”
- selenium - 尽管将焦点切换到它,但无法在弹出窗口中定位元素,硒
- reporting-services - 当您分离然后重新附加项目集合时,TFS 2015 报告服务器会发生什么情况?
- css - 如何淡出渐变中的背景图像,以便看到其下方的元素?
- machine-learning - 泰坦尼克号:灾难中的机器学习
- css - CSS 变换比例截断图像
- logstash - 如何使用动态索引映射将 CSV 提供给 logstash
- c++ - 如何在 C++ 中建立远程 Sql Server 连接?
- xml - 阅读 XML 注释代码时 - 感叹号 (!) 正在产生问题
- angular - 如果刷新,带有共享模块的 Angular 5 和 ngx-translate 会松散翻译