Python Learner
Python Learner

Reputation: 33

Filter the data for particular 15 days of all years in django

I am trying to print the data for particular 15 days of every year.

For example to get the Employee's details who has birthdays with in 15 days.

today = datetime.now()
start_day = today.day
start_month = today.month
end_day = today + timedelta(days=15)
end_date = end_day.day
end_month = end_day.month
user_dob_obj = UserProfile.objects.filter(Q(date_of_birth__month__gte=start_month, date_of_birth__day__gte=start_day) &
         Q(date_of_birth__month__lte=end_month, date_of_birth__day__lte=end_date))

Upvotes: 2

Views: 101

Answers (1)

almost a beginner
almost a beginner

Reputation: 1632

Update

Sorry I misunderstood your question. You can use if statement to check if the month is the same 15 days later. Then use the or logical operation to make sure birthdays in current and next month are filtered.

today = datetime.now()
end_date = today + timedelta(days=15)
if today.month == end_date.month:
    user_dob_obj = user_dob_obj.filter(date_of_birth__month=today.month, date_of_birth__day__gte=today.day, date_of_birth__day__lte=end_date.day)
else:
    user_dob_obj = queryset.filter(Q(date_of_birth__month=today.month, date_of_birth__day__gte=today.day) | Q(date_of_birth__month=end_date.month, date_of_birth__day__lte=end_date.day))

Upvotes: 2

Related Questions