0

How can I check that, there is another day from my date variable? I mean, how to write if statement between my DateField, and actuall date to check is is another day (for example: last was 26 July, now is 27, so i want to show alert box).

ziomagic
  • 93
  • 1
  • 9
  • Show some input and desired output. – Nishant Nawarkhede Sep 22 '14 at 17:37
  • Can you specify where exactly you are trying to match the Datefield ? Is it in HTML page or in views.py ? Need few more details like from where you are fetching the datefield – d-coder Sep 22 '14 at 17:40
  • I want add free points to user every day he log in. No mather what time is it, its giving points every day, so I need condition which will check is it another day, from his "last_points_added" datefield. – ziomagic Sep 22 '14 at 17:54

1 Answers1

1

You can do something like this:

from datetime import datetime, timedelta

def some_view(request):
    now = datetime.now()
    # using a timedelta avoids having to know about length of months etc:
    same_time_tomorrow = now + timedelta(days=1)
    start_of_tomorrow = same_time_tomorrow.replace(
        hour=0,
        minute=0,
        second=0,
        microsecond=0
    )
    # returns a timedelta object:
    difference = start_of_tomorrow - request.user.last_points_added
    if difference.days > 0:
        # add points
Anentropic
  • 32,188
  • 12
  • 99
  • 147