2

In my django app, I have some date. I need to count how many months left to this date, using full (rounded) months. eg: today is 19/02/2015 (february), my "search" date is 04/08/2015. Difference should be 6. How can I get a proper value?

dease
  • 2,975
  • 13
  • 39
  • 75
  • @Mark R.: I disagree, when you read the questions carefully, there are important differences! – Juergen Feb 20 '15 at 00:51
  • [This post](https://www.odoo.com/forum/help-1/question/how-do-i-calculate-number-of-months-between-two-dates-9443) nails it! Use `dateutil.relativedelta`. – srodriguex Jun 04 '16 at 15:22

3 Answers3

1

I like the arrow library: http://crsmithdev.com/arrow/

eg.

 d1 = arrow.get("19/02/2015", "DD/MM/YYYY")
 d2 = arrow.get("04/08/2015", "DD/MM/YYYY")

 (d2-d1).days

You are going to have decide how to do your calculation. Divide by 30 or extract the months and subtract those.

d2.month - d1.month

To handle it going over a year:

 ((d2.year * 100) + d2.month) - ((d1.year * 100) + d1.month)
PhoebeB
  • 8,434
  • 8
  • 57
  • 76
1
from datetime import datetime,timedelta
from calendar import monthrange

today = datetime.today()
dt = "04/08/2015"

fut = datetime.strptime(dt, "%d/%m/%Y")
diff = 0
while today <= fut:
    today += timedelta(days=monthrange(today.day,today.month)[1])
    diff += 1
print(diff)
6

Without importing calender we can increment a count everytime we see a new month:

from datetime import datetime,timedelta

today = datetime.today() 
dt = "09/08/2015"

fut = datetime.strptime(dt, "%d/%m/%Y")
diff = 0
while today <= fut:
    mon = today.month
    today += timedelta(days=1)
    if today.month != mon:
        diff += 1
print(diff)
6

If you want to make the future day the last day of the month:

from datetime import datetime, timedelta
from calendar import monthrange

today = datetime.today()
dt = "02/08/2015"

fut = datetime.strptime(dt, "%d/%m/%Y")
fut = fut + timedelta(days=monthrange(fut.day,fut.month)[1]-fut.day)
diff = 0
while today < fut:
    mon = today.month
    today += timedelta(days=1)
    if today.month != mon:
        diff += 1
print(diff)

This is purposely inaccurate to allow for rounding as required, all we care about are the amount of different months we encounter.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

To calculate the month difference (rounded) I would go this direction:

  • Get the date objects for the different dates (see datetime package). This is rather easy, since the constructor takes year, month, day
  • Calculate the difference between the dates "date2 - date1" this automatically gives a timedelta object
  • Get the difference seconds between the two dates by calling "total_seconds()" on the timedelta object
  • Dividing the number of seconds by 24*60*60 will give the number of days
  • Dividing the number of days by 30 or (as you like) 31 will give the number of months. You can round the value as you like.

This should suffice:

    d,m,y = date1.split('/')
    d1 = datetime.date(y, m, d)
    d,m,y = date1.split('/')
    d2 = datetime.date(y, m, d)
    delta = d2 - d1
    days  = delta.total_seconds() // (24*60*60)
    result = int(days/30.0+0.5)

The nice thing: No additional packages needed, all is in the standard packages.

Juergen
  • 12,378
  • 7
  • 39
  • 55