Simple way to find the amount of years between two dates?

28 views
Skip to first unread message

Brandon Keith Biggs

unread,
Jul 12, 2015, 7:02:55 AM7/12/15
to django...@googlegroups.com
Hello,
using the django.utils.timezone.now() function, how can I find the difference from another date in years?
For example, if I have two dates like:
date1 = (1991, 12, 23)
date2 = (1995, 12, 6)

How do I use the current time to get 23 from the first and 19 from the second?
thank you,

Avraham Serour

unread,
Jul 12, 2015, 7:46:07 AM7/12/15
to django...@googlegroups.com

date 1 - date 2


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55A20F97.8070106%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

lars van Gemerden

unread,
Jul 12, 2015, 10:26:27 AM7/12/15
to django...@googlegroups.com
question is a bit vague, but look at python datetime.datetime and datetime.timedelta

Brandon Keith Biggs

unread,
Jul 12, 2015, 11:46:10 AM7/12/15
to django...@googlegroups.com
Hello,
This works:
from django.utils import timezone
from datetime import datetime

today = timezone.now()
date1 = datetime(1991, 12, 23, tzinfo=today.tzinfo)
apart = today-date1
years = apart.days/365
print(years)
This works great for what I need it for!
thanks,

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages