首页 > 解决方案 > 用于在字典中给定日期前 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")

标签: pythonemailtimereminders

解决方案


您需要转换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")

推荐阅读