I am using the datetime Python module. I am looking to calculate the date 6 months from the current date. Could someone give me a little help doing this?
The reason I want to generate a date 6 months from the current date is to produce a Review Date. If the user enters data into the system it will have a review date of 6 months from the date they entered the data.
I have a better way to solve the 'February 31st' problem:
I think that it also works with negative numbers (to subtract months), but I haven't tested this very much.
How about this? Not using another library (
dateutil
) ortimedelta
? building on vartec's answer I did this and I believe it works:I tried using
timedelta
, but because it is counting the days,365/2
or6*356/12
does not always translate to 6 months, but rather 182 days. e.g.I believe that we usually assume that 6 month's from a certain day will land on the same day of the month but 6 months later (i.e.
2015-03-10
-->2015-09-10
, Not2015-09-08
)I hope you find this helpful.
General function to get next date after/before x months.
I found this solution to be good. (This uses the python-dateutil extension)
The advantage of this approach is that it takes care of issues with 28, 30, 31 days etc. This becomes very useful in handling business rules and scenarios (say invoice generation etc.)