Date 6 months into the future

I am using the dythetime Python module in django. I am calculating to calculate the date if the expiration date is less than or equal to 6 months from today's date.

The reason I want to create a date 6 months from the current date is to set an alert that will highlight the field / column where this event occurs. I don't know if my question is clear. I read about the timedelta function, but cant really get my head around it. I am trying to write an if statement to fulfill this condition. Can anyone help me? I am new to django and python.

+3


source to share


3 answers


There are two approaches, one is only slightly imprecise, one imprecise in a different way:

  • Add datetime.timedelta()

    from 365.25 / 2 days (the average length of the year is divisible by two):

    import datetime
    sixmonths = datetime.datetime.now() + datetime.timedelta(days=365.25/2)
    
          

    This method will give you a datetime stamp of 6 months in the future where we define 6 monhs for exactly half a year (on average).

  • Use an external library dateutil

    , it has a great relativedelta

    one that will add 6 months based on calendar calculations to today's date:

    import datetime
    from dateutil.relativedelat import relativedelta
    sixmonths = datetime.datetime.now() + relativedelta(months=6)
    
          

    This method will give you a datetime stamp for 6 months in the future when the month component of the date is sent to 6 and it will respect the monthly boundaries without crossing over. For example, August 30 plus 6 months - February 28 or 29 (possibly in case of a leap year).

A demo might be helpful. In my timezone, at the time of posting, this means:

>>> import datetime
>>> from dateutil.relativedelta import relativedelta
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2013, 2, 18, 12, 16, 0, 547567)
>>> now + datetime.timedelta(days=365.25/2)
datetime.datetime(2013, 8, 20, 3, 16, 0, 547567)
>>> now + relativedelta(months=6)
datetime.datetime(2013, 8, 18, 12, 16, 0, 547567)

      



Thus, there is a difference of 1 and 15 hours between the two methods.

The same methods work fine with objects datetime.date

:

>>> today = datetime.date.today()
>>> today
datetime.date(2013, 2, 18)
>>> today + datetime.timedelta(days=365.25/2)
datetime.date(2013, 8, 19)
>>> today + relativedelta(months=6)
datetime.date(2013, 8, 18)

      

Semi-annual timedelta becomes less accurate when applied only to dates (the 5/8 day delta component is ignored).

+6


source


If by "6 ​​months" you mean 180 days, you can use:

import datetime
d = datetime.date.today()
d + datetime.timedelta(6 * 30)

      



Alternatively, if you are referring to the actual 6 months of the calendar, you will have to look for the calendar module and do some checks every month. For example:

import datetime
import calendar

def add_6_months(a_date):
    month = a_date.month - 1 + 6
    year = a_date.year + month / 12
    month = month % 12 + 1
    day = min(a_date.day,calendar.monthrange(year, month)[1])
    return datetime.date(year, month, day)

      

+1


source


I would give Delorean a serious look. It is built on top dateutil

and pytz

to do what you asked, just the following.

>>> d =  Delorean()
>>> d
Delorean(datetime=2013-02-21 06:00:21.195025+00:00, timezone=UTC)
>>> d.next_month(6)
Delorean(datetime=2013-08-21 06:00:21.195025+00:00, timezone=UTC)

      

It takes into account all dateutil calculations and also provides an interface for timezone shifting. to get the required time and time of a simple .datetime

Delorean object.

0


source







All Articles