Save old dates and display time until next anniversary of that date in Django
NickName:berserkia Ask DateTime:2013-10-18T09:16:39

Save old dates and display time until next anniversary of that date in Django

I'm trying to write a basic reminder app where users can enter important dates (birthdays, anniversaries, etc.) and see how much time is left until the next anniversary of that date. For example, a user can have a birthday as something like Jan 1st 1990, and I want to display for them the time until their next birthday (2 months 14 days).

I've been using the django timeuntil built in template tag, but it only works for dates in the future (it won't display anything if the date is before the current date). I'm not sure how to to go about "normalizing" the entered date to be a time in the future.

Current code:

def events(request):
    relationships = Relationship.objects.filter(user=request.user)
     events = Event.objects.filter(user=request.user).order_by('date')[:8]
    event_date = events[0].date

    if datetime.now() >= event_date:
        difference = datetime.now() - event_date
        event_date_new = event_date + difference
        event_date = event_date_new
     context = {
         'relationships': relationships,
         'events': events
      }
     return render(request, 'app/events.html', context)     

Template

<td class="column-right"><h4>{{event.date|timeuntil}}</h4></td>

(It also throws an error on datetime.now() module has no attribute "now", is that an old way to find the current date?)

Thanks in advance!

Copyright Notice:Content Author:「berserkia」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19440005/save-old-dates-and-display-time-until-next-anniversary-of-that-date-in-django

Answers
Peter DeGlopper 2013-10-18T01:26:58

now is a method of the datetime.datetime class, not the datetime module:\n\ndifference = datetime.datetime.now() - event_date\n\n\nThat said, your correction logic is off. If you write out the calculation in one line, it should be clear why:\n\ndifference = datetime.now() - event_date\nevent_date_new = event_date + difference\n\n\nSo by substitution:\n\nevent_date_new = event_date + (datetime.now() - event_date)\n\n\nWhich simplifies to:\n\nevent_date_new = datetime.now()\n\n\nI would try it this way:\n\nnow = datetime.datetime.now()\nevent_date = event_date.replace(year=now.year)\nif event_date < now:\n event_date.replace(year=now.year + 1)\n\n\nAs Christian Ternus points out, that won't work if event_date is a leap day. You could disallow leap days as event_date values, requiring the user to enter either February 28th or March 1st. Or you could catch the ValueError exception raised by replace and make the correction, adding or subtracting a one-day timedelta. Or use calendar.isleap to figure out the next leap year, if you want to show the true time interval.",


More about “Save old dates and display time until next anniversary of that date in Django” related questions

Save old dates and display time until next anniversary of that date in Django

I'm trying to write a basic reminder app where users can enter important dates (birthdays, anniversaries, etc.) and see how much time is left until the next anniversary of that date. For example, a...

Show Detail

Display employee anniversary dates within the next month or year of current date

basically trying to create a query that will display employee anniversary dates for upcoming month or year of current date, also would like to display a column that shows the years of service SELECT

Show Detail

Calculating next 3 year anniversary of employee based on join date

I have an Employee mysql db table with employee name and start date. I want to be able to calculate upcoming 3 year anniversary of each employee from today's date. (So for example, Jordan's next up...

Show Detail

Filter a model by date range of next week in Django

How can I filter the model data according to date range of two columns - birthday and anniversary which are datefields using Django querysets? This is in a function that is called every friday. So ...

Show Detail

Generate next three month anniversary date

I have a table that contains details on charitable donations and the date they were added to the database. I am trying to write a query that will display the next date after today that will be a t...

Show Detail

EDATE comparable in SQL Server? -- Next Anniversary Date Calculation

Can someone help me figure out a formula to be used in SQL Server similar to the Excel formula pasted below? =EDATE(date,(DATEDIF(date,TODAY(),"y")+1)*12) I want to capture the next anniversary d...

Show Detail

Oracle SQL Anniversary Dates Query

I need some assistance in creating a query which shows the anniversary of employees in the business. I need the report to run and show results within two dates I select. I would like the report to ...

Show Detail

Generate sequences of anniversary dates between 2 dates

I tried to generate a sequence of dates between two dates. By search all the old posts, I found very nice solution using seq.Date. For example: &gt; seq.Date(as.Date("2016/1/15"), as.Date("2016/5...

Show Detail

Offset Anniversary Date

I have an anniversary date in C2, and other anniversary date from E3 to E6. I want that if Anniversary date &lt; Today then the C2 value is the next anniversary Date. With this exemple, if today...

Show Detail

Calculate the next anniversary date after today

What's the quickest/neatest way to calculate the next anniversary of someone's birthday. For example, if I knew a person was born on 31st January, 1990, and today is the 10th February 2000, their ...

Show Detail