Reputation: 3941
I have users who get a special offer for 6 months on registration. I save the datetime of the registration and know that this datetime + 6 months is the special offer.
Now I need after 5 month to display a button which reminds them that the offer is running out in 1 month.
So I need to check whether datetime.now() is within range:
user1 = datetime(2017,4,30)
user2 = datetime(2017,5,25)
user3 = datetime(2017,1,11)
user4 = datetime(2017,2,20)
user5 = datetime(2017,12,23)
user6 = datetime(2017,7,22)
list_of_users = [user1,user2,user3,user4,user5,user6]
for user_date in list_of_users:
if datetime.now() < user_date + relativedelta(months=6) and datetime.now() > user_date + relativedelta(months=5):
print user_date, "yes"
I have created some examples and user5 should be within this range, but somehow it wont recognize it.
Upvotes: 0
Views: 51
Reputation: 1121924
Your code works just fine, albeit a bit verbose. user5
does not qualify your criteria, the data is 5+ months in the future, not in the past. Their special offer period has not yet started, let alone be 1 month away from expiring. They should only see the warning between the 23rd of May and June of 2018.
You can more concisely test by storing the cut-off dates first, and using chained comparisons:
now = datetime.now()
started_min, started_max = now - relativedelta(months=6), now - relativedelta(months=5)
for user_date in list_of_users:
if started_min <= user_date <= started_max:
print user_date, "yes"
So if a user started their offer between 5 and 6 months ago, they need to be reminded now, because they are in their last month.
Upvotes: 2