Search code examples
pythondjangodatefield

Django. Check if its another day


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).


Solution

  • 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