vishavjeet

singh.vishavjeet11@gmail.com

Python script for find next monitoring/appointment/birthday ect

Posted on April 07, 2022



Using Function


from datetime import datetime, timedelta

def get_remaining_days(review_date, frequency):
date_formate = '%Y-%m-%d'
review_date = datetime.strptime(str(review_date), date_formate)
today_date = datetime.strptime(str(datetime.now()).split(" ")[0], date_formate)

difference = today_date - review_date
if difference.days <= 0:
next_monitoring = review_date + timedelta(days=frequency)
else:
flag = True
next_monitoring = review_date + timedelta(days=0)
while flag:
next_monitoring = next_monitoring + timedelta(days=frequency)
difference = next_monitoring - today_date
if difference.days >= 0:
flag = False
remaining = next_monitoring - today_date

print(f'Next Monitoring date will be {next_monitoring} or after {remaining.days} days')
return remaining.days, next_monitoring


get_remaining_days("2022-04-15", 20)


Using Class


from datetime import datetime, timedelta
class RemaningDayFinder():

DATETIME_FORMAT = '%Y-%m-%d'

def __init__(self, review_date, frequency):
self.review_date = datetime.strptime(str(review_date), self.DATETIME_FORMAT)
self.today_date = datetime.strptime(str(datetime.now()).split(" ")[0], self.DATETIME_FORMAT)
self.frequency = frequency

def get_remaining_days(self):

difference = self.today_date - self.review_date

if difference.days <= 0:
next_monitoring = self.review_date + timedelta(days=self.frequency)
else:
flag = True
next_monitoring = self.review_date + timedelta(days=0)
while flag:
next_monitoring = next_monitoring + timedelta(days=self.frequency)
difference = next_monitoring - self.today_date
if difference.days >= 0:
flag = False

remaining = next_monitoring - self.today_date
return remaining.days, next_monitoring

def __str__(self):
remaining_days, next_monitoring = self.get_remaining_days()
return f'Next Monitoring date will be {next_monitoring} or after {remaining_days} days'


remaining_days = RemaningDayFinder("2022-04-15", 20)
print(remaining_days)


Best Logic Based on Performance


def get_remaining_days(review_date, frequency):
date_formate = '%Y-%m-%d'
review_date = datetime.strptime(str(review_date), date_formate)
today_date = datetime.strptime(str(datetime.now()).split(" ")[0], date_formate)


difference = today_date - review_date
if difference.days <= 0:
next_monitoring = review_date + timedelta(days=frequency)
else:
pass_days = ((difference.days//frequency)*frequency)+frequency
next_monitoring = review_date + timedelta(days=pass_days)

remaining = next_monitoring - today_date

print(f'Next Monitoring date will be {next_monitoring} or after {remaining.days} days')
return remaining.days, next_monitoring


Sign In for comment and like the post.