Find day difference between two datetimes (excluding weekend days) in Python? [duplicate] - python

This question already has answers here:
Number of days between 2 dates, excluding weekends
(22 answers)
Closed 7 years ago.
The same problem to Find day difference between two dates (excluding weekend days) but it is for javascript. How to do that in Python?

Try it with scikits.timeseries:
import scikits.timeseries as ts
import datetime
a = datetime.datetime(2011,8,1)
b = datetime.datetime(2011,8,29)
diff_business_days = ts.Date('B', b) - ts.Date('B', a)
# returns 20
or with dateutil:
import datetime
from dateutil import rrule
a = datetime.datetime(2011,8,1)
b = datetime.datetime(2011,8,29)
diff_business_days = len(list(rrule.rrule(rrule.DAILY,
dtstart=a,
until=b - datetime.timedelta(days=1),
byweekday=(rrule.MO, rrule.TU, rrule.WE, rrule.TH, rrule.FR))))
scikits.timeseries look depricated : http://pytseries.sourceforge.net/
With pandas instead someone can do :
import pandas as pd
a = datetime.datetime(2015, 10, 1)
b = datetime.datetime(2015, 10, 29)
diff_calendar_days = pd.date_range(a, b).size
diff_business_days = pd.bdate_range(a, b).size

Not sure that this is the best one solution but it works for me:
from datetime import datetime, timedelta
startDate = datetime(2011, 7, 7)
endDate = datetime(2011, 10, 7)
dayDelta = timedelta(days=1)
diff = 0
while startDate != endDate:
if startDate.weekday() not in [5,6]:
diff += 1
startDate += dayDelta

Here's a O(1) complexity class solution which uses only built-in Python libraries.
It has constant performance regardless of time interval length and doesn't care about argument order.
#
# by default, the last date is not inclusive
#
def workdaycount(first, second, inc = 0):
if first == second:
return 0
import math
if first > second:
first, second = second, first
if inc:
from datetime import timedelta
second += timedelta(days=1)
interval = (second - first).days
weekspan = int(math.ceil(interval / 7.0))
if interval % 7 == 0:
return interval - weekspan * 2
else:
wdf = first.weekday()
if (wdf < 6) and ((interval + wdf) // 7 == weekspan):
modifier = 0
elif (wdf == 6) or ((interval + wdf + 1) // 7 == weekspan):
modifier = 1
else:
modifier = 2
return interval - (2 * weekspan - modifier)
#
# sample usage
#
print workdaycount(date(2011, 8, 15), date(2011, 8, 22)) # returns 5
print workdaycount(date(2011, 8, 15), date(2011, 8, 22), 1) # last date inclusive, returns 6

Related

Python - Count number of date/time iterations between 2 dates using freq

I am trying to figure out how to calculate number of iterations of datetimes between 2 dates using a specific frequency (1D 3D 3H 15T)
for example:
freq = '3H'
start = datetime.datetime(2018, 8, 14, 9, 0, 0)
end= datetime.datetime(2018, 8, 15)
total = func(start, end, freq)
if freq = '3H' total would be 5.
if freq = '30T' total would be 30
what would func look like?
EDIT
I'm leaving my original question up there, and adding details I failed to add originally in order to keep things as simple as possible.
In the code I am working on, I have a Pandas DataFrame with a DateTimeIndex. I needed to calculate the number of rows since a specific time(above). I thought about creating a DataFrame starting from that time and filling in the gaps, and counting rows like that, but that seems silly now.
the function I ended up using (with the parsing) is this:
def periods(row, time_frame):
start = datetime.datetime(2018, 8, 14, 9, 0, 0)
end = row.name
t = time_frame[-1:]
n = int(re.findall('\d+',time_frame)[0])
if t is 'H':
freq = datetime.timedelta(hours=n)
elif t is 'T':
freq = datetime.timedelta(minutes=n)
else:
freq = datetime.timedelta(days=n)
count = 0
while start < end:
start += freq
count += 1
return count
and I call it from my dataframe(candlesticks) like this:
candlesticks['n'] = candlesticks.apply(lambda x: periods(x, candlesticks.index.freqstr), axis=1)
Use the timedelta module in the datetime library, and from there it's the same as comparing numbers essentially.
from datetime import timedelta
freq = timedelta(hours=3)
def periods(frequency, start, end):
count = 0
while start < end:
start += frequency
count += 1
return count
p = periods(freq, start, end)
print(p)
>> 5

Is there a python DateDelta? [duplicate]

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 found this solution to be good. (This uses the python-dateutil extension)
from datetime import date
from dateutil.relativedelta import relativedelta
six_months = date.today() + relativedelta(months=+6)
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.)
$ date(2010,12,31)+relativedelta(months=+1)
datetime.date(2011, 1, 31)
$ date(2010,12,31)+relativedelta(months=+2)
datetime.date(2011, 2, 28)
Well, that depends what you mean by 6 months from the current date.
Using natural months:
inc = 6
year = year + (month + inc - 1) // 12
month = (month + inc - 1) % 12 + 1
Using a banker's definition, 6*30:
date += datetime.timedelta(6 * 30)
With Python 3.x you can do it like this:
from datetime import datetime, timedelta
from dateutil.relativedelta import *
date = datetime.now()
print(date)
# 2018-09-24 13:24:04.007620
date = date + relativedelta(months=+6)
print(date)
# 2019-03-24 13:24:04.007620
but you will need to install python-dateutil module:
pip install python-dateutil
So, here is an example of the dateutil.relativedelta which I found useful for iterating through the past year, skipping a month each time to the present date:
>>> import datetime
>>> from dateutil.relativedelta import relativedelta
>>> today = datetime.datetime.today()
>>> month_count = 0
>>> while month_count < 12:
... day = today - relativedelta(months=month_count)
... print day
... month_count += 1
...
2010-07-07 10:51:45.187968
2010-06-07 10:51:45.187968
2010-05-07 10:51:45.187968
2010-04-07 10:51:45.187968
2010-03-07 10:51:45.187968
2010-02-07 10:51:45.187968
2010-01-07 10:51:45.187968
2009-12-07 10:51:45.187968
2009-11-07 10:51:45.187968
2009-10-07 10:51:45.187968
2009-09-07 10:51:45.187968
2009-08-07 10:51:45.187968
As with the other answers, you have to figure out what you actually mean by "6 months from now." If you mean "today's day of the month in the month six years in the future" then this would do:
datetime.datetime.now() + relativedelta(months=6)
For beginning of month to month calculation:
from datetime import timedelta
from dateutil.relativedelta import relativedelta
end_date = start_date + relativedelta(months=delta_period) + timedelta(days=-delta_period)
Python can use datautil package for that, Please see the example below
It's not Just limited to that, you can pass combination of days, Months and Years at the same time also.
import datetime
from dateutil.relativedelta import relativedelta
# subtract months
proc_dt = datetime.date(2021,8,31)
proc_dt_minus_3_months = proc_dt + relativedelta(months=-3)
print(proc_dt_minus_3_months)
# add months
proc_dt = datetime.date(2021,8,31)
proc_dt_plus_3_months = proc_dt + relativedelta(months=+3)
print(proc_dt_plus_3_months)
# subtract days:
proc_dt = datetime.date(2021,8,31)
proc_dt_minus_3_days = proc_dt + relativedelta(days=-3)
print(proc_dt_minus_3_days)
# add days days:
proc_dt = datetime.date(2021,8,31)
proc_dt_plus_3_days = proc_dt + relativedelta(days=+3)
print(proc_dt_plus_3_days)
# subtract years:
proc_dt = datetime.date(2021,8,31)
proc_dt_minus_3_years = proc_dt + relativedelta(years=-3)
print(proc_dt_minus_3_years)
# add years:
proc_dt = datetime.date(2021,8,31)
proc_dt_plus_3_years = proc_dt + relativedelta(years=+3)
print(proc_dt_plus_3_years)
Results:
2021-05-31
2021-11-30
2021-08-28
2021-09-03
2018-08-31
2024-08-31
This solution works correctly for December, which most of the answers on this page do not.
You need to first shift the months from a 1-based index (ie Jan = 1) to a 0-based index (ie Jan = 0) before using modulus ( % ) or integer division ( // ), otherwise November (11) plus 1 month gives you 12, which when finding the remainder ( 12 % 12 ) gives 0.
(And dont suggest "(month % 12) + 1" or Oct + 1 = december!)
def AddMonths(d,x):
newmonth = ((( d.month - 1) + x ) % 12 ) + 1
newyear = int(d.year + ((( d.month - 1) + x ) / 12 ))
return datetime.date( newyear, newmonth, d.day)
However ... This doesnt account for problem like Jan 31 + one month. So we go back to the OP - what do you mean by adding a month? One solution is to backtrack until you get to a valid day, given that most people would presume the last day of jan, plus one month, equals the last day of Feb.
This will work on negative numbers of months too.
Proof:
>>> import datetime
>>> AddMonths(datetime.datetime(2010,8,25),1)
datetime.date(2010, 9, 25)
>>> AddMonths(datetime.datetime(2010,8,25),4)
datetime.date(2010, 12, 25)
>>> AddMonths(datetime.datetime(2010,8,25),5)
datetime.date(2011, 1, 25)
>>> AddMonths(datetime.datetime(2010,8,25),13)
datetime.date(2011, 9, 25)
>>> AddMonths(datetime.datetime(2010,8,25),24)
datetime.date(2012, 8, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-1)
datetime.date(2010, 7, 25)
>>> AddMonths(datetime.datetime(2010,8,25),0)
datetime.date(2010, 8, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-12)
datetime.date(2009, 8, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-8)
datetime.date(2009, 12, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-7)
datetime.date(2010, 1, 25)>>>
What do you mean by "6 months"?
Is 2009-02-13 + 6 months == 2009-08-13? Or is it 2009-02-13 + 6*30 days?
import mx.DateTime as dt
#6 Months
dt.now()+dt.RelativeDateTime(months=6)
#result is '2009-08-13 16:28:00.84'
#6*30 days
dt.now()+dt.RelativeDateTime(days=30*6)
#result is '2009-08-12 16:30:03.35'
More info about mx.DateTime
This doesn't answer the specific question (using datetime only) but, given that others suggested the use of different modules, here there is a solution using pandas.
import datetime as dt
import pandas as pd
date = dt.date.today() - \
pd.offsets.DateOffset(months=6)
print(date)
2019-05-04 00:00:00
Which works as expected in leap years
date = dt.datetime(2019,8,29) - \
pd.offsets.DateOffset(months=6)
print(date)
2019-02-28 00:00:00
There's no direct way to do it with Python's datetime.
Check out the relativedelta type at python-dateutil. It allows you to specify a time delta in months.
I know this was for 6 months, however the answer shows in google for "adding months in python" if you are adding one month:
import calendar
date = datetime.date.today() //Or your date
datetime.timedelta(days=calendar.monthrange(date.year,date.month)[1])
this would count the days in the current month and add them to the current date, using 365/12 would ad 1/12 of a year can causes issues for short / long months if your iterating over the date.
Just use the timetuple method to extract the months, add your months and build a new dateobject. If there is a already existing method for this I do not know it.
import datetime
def in_the_future(months=1):
year, month, day = datetime.date.today().timetuple()[:3]
new_month = month + months
return datetime.date(year + (new_month / 12), (new_month % 12) or 12, day)
The API is a bit clumsy, but works as an example. Will also obviously not work on corner-cases like 2008-01-31 + 1 month. :)
Using Python standard libraries, i.e. without dateutil or others, and solving the 'February 31st' problem:
import datetime
import calendar
def add_months(date, months):
months_count = date.month + months
# Calculate the year
year = date.year + int(months_count / 12)
# Calculate the month
month = (months_count % 12)
if month == 0:
month = 12
# Calculate the day
day = date.day
last_day_of_month = calendar.monthrange(year, month)[1]
if day > last_day_of_month:
day = last_day_of_month
new_date = datetime.date(year, month, day)
return new_date
Testing:
>>>date = datetime.date(2018, 11, 30)
>>>print(date, add_months(date, 3))
(datetime.date(2018, 11, 30), datetime.date(2019, 2, 28))
>>>print(date, add_months(date, 14))
(datetime.date(2018, 12, 31), datetime.date(2020, 2, 29))
Dateutil package has implementation of such functionality. But be aware, that this will be naive, as others pointed already.
I have a better way to solve the 'February 31st' problem:
def add_months(start_date, months):
import calendar
year = start_date.year + (months / 12)
month = start_date.month + (months % 12)
day = start_date.day
if month > 12:
month = month % 12
year = year + 1
days_next = calendar.monthrange(year, month)[1]
if day > days_next:
day = days_next
return start_date.replace(year, month, day)
I think that it also works with negative numbers (to subtract months), but I haven't tested this very much.
A quick suggestion is Arrow
pip install arrow
>>> import arrow
>>> arrow.now().date()
datetime.date(2019, 6, 28)
>>> arrow.now().shift(months=6).date()
datetime.date(2019, 12, 28)
The QDate class of PyQt4 has an addmonths function.
>>>from PyQt4.QtCore import QDate
>>>dt = QDate(2009,12,31)
>>>required = dt.addMonths(6)
>>>required
PyQt4.QtCore.QDate(2010, 6, 30)
>>>required.toPyDate()
datetime.date(2010, 6, 30)
Modified the AddMonths() for use in Zope and handling invalid day numbers:
def AddMonths(d,x):
days_of_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
newmonth = ((( d.month() - 1) + x ) % 12 ) + 1
newyear = d.year() + ((( d.month() - 1) + x ) // 12 )
if d.day() > days_of_month[newmonth-1]:
newday = days_of_month[newmonth-1]
else:
newday = d.day()
return DateTime( newyear, newmonth, newday)
import time
def add_month(start_time, months):
ret = time.strptime(start_time, '%Y-%m-%d')
t = list(ret)
t[1] += months
if t[1] > 12:
t[0] += 1 + int(months / 12)
t[1] %= 12
return int(time.mktime(tuple(t)))
Modified Johannes Wei's answer in the case 1new_month = 121. This works perfectly for me. The months could be positive or negative.
def addMonth(d,months=1):
year, month, day = d.timetuple()[:3]
new_month = month + months
return datetime.date(year + ((new_month-1) / 12), (new_month-1) % 12 +1, day)
How about this? Not using another library (dateutil) or timedelta?
building on vartec's answer I did this and I believe it works:
import datetime
today = datetime.date.today()
six_months_from_today = datetime.date(today.year + (today.month + 6)/12, (today.month + 6) % 12, today.day)
I tried using timedelta, but because it is counting the days, 365/2 or 6*356/12 does not always translate to 6 months, but rather 182 days. e.g.
day = datetime.date(2015, 3, 10)
print day
>>> 2015-03-10
print (day + datetime.timedelta(6*365/12))
>>> 2015-09-08
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, Not 2015-09-08)
I hope you find this helpful.
import datetime
'''
Created on 2011-03-09
#author: tonydiep
'''
def add_business_months(start_date, months_to_add):
"""
Add months in the way business people think of months.
Jan 31, 2011 + 1 month = Feb 28, 2011 to business people
Method: Add the number of months, roll back the date until it becomes a valid date
"""
# determine year
years_change = months_to_add / 12
# determine if there is carryover from adding months
if (start_date.month + (months_to_add % 12) > 12 ):
years_change = years_change + 1
new_year = start_date.year + years_change
# determine month
work = months_to_add % 12
if 0 == work:
new_month = start_date.month
else:
new_month = (start_date.month + (work % 12)) % 12
if 0 == new_month:
new_month = 12
# determine day of the month
new_day = start_date.day
if(new_day in [31, 30, 29, 28]):
#user means end of the month
new_day = 31
new_date = None
while (None == new_date and 27 < new_day):
try:
new_date = start_date.replace(year=new_year, month=new_month, day=new_day)
except:
new_day = new_day - 1 #wind down until we get to a valid date
return new_date
if __name__ == '__main__':
#tests
dates = [datetime.date(2011, 1, 31),
datetime.date(2011, 2, 28),
datetime.date(2011, 3, 28),
datetime.date(2011, 4, 28),
datetime.date(2011, 5, 28),
datetime.date(2011, 6, 28),
datetime.date(2011, 7, 28),
datetime.date(2011, 8, 28),
datetime.date(2011, 9, 28),
datetime.date(2011, 10, 28),
datetime.date(2011, 11, 28),
datetime.date(2011, 12, 28),
]
months = range(1, 24)
for start_date in dates:
for m in months:
end_date = add_business_months(start_date, m)
print("%s\t%s\t%s" %(start_date, end_date, m))
Rework of an earlier answer by user417751. Maybe not so pythonic way, but it takes care of different month lengths and leap years. In this case 31 January 2012 + 1 month = 29 February 2012.
import datetime
import calendar
def add_mths(d, x):
newday = d.day
newmonth = (((d.month - 1) + x) % 12) + 1
newyear = d.year + (((d.month - 1) + x) // 12)
if newday > calendar.mdays[newmonth]:
newday = calendar.mdays[newmonth]
if newyear % 4 == 0 and newmonth == 2:
newday += 1
return datetime.date(newyear, newmonth, newday)
Yet another solution - hope someone will like it:
def add_months(d, months):
return d.replace(year=d.year+months//12).replace(month=(d.month+months)%12)
This solution doesn't work for days 29,30,31 for all cases, so more robust solution is needed (which is not so nice anymore :) ):
def add_months(d, months):
for i in range(4):
day = d.day - i
try:
return d.replace(day=day).replace(year=d.year+int(months)//12).replace(month=(d.month+int(months))%12)
except:
pass
raise Exception("should not happen")
From this answer, see parsedatetime. Code example follows. More details: unit test with many natural-language -> YYYY-MM-DD conversion examples, and apparent parsedatetime conversion challenges/bugs.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time, calendar
from datetime import date
# from https://github.com/bear/parsedatetime
import parsedatetime as pdt
def print_todays_date():
todays_day_of_week = calendar.day_name[date.today().weekday()]
print "today's date = " + todays_day_of_week + ', ' + \
time.strftime('%Y-%m-%d')
def convert_date(natural_language_date):
cal = pdt.Calendar()
(struct_time_date, success) = cal.parse(natural_language_date)
if success:
formal_date = time.strftime('%Y-%m-%d', struct_time_date)
else:
formal_date = '(conversion failed)'
print '{0:12s} -> {1:10s}'.format(natural_language_date, formal_date)
print_todays_date()
convert_date('6 months')
The above code generates the following from a MacOSX machine:
$ ./parsedatetime_simple.py
today's date = Wednesday, 2015-05-13
6 months -> 2015-11-13
$
Here's a example which allows the user to decide how to return a date where the day is greater than the number of days in the month.
def add_months(date, months, endOfMonthBehaviour='RoundUp'):
assert endOfMonthBehaviour in ['RoundDown', 'RoundIn', 'RoundOut', 'RoundUp'], \
'Unknown end of month behaviour'
year = date.year + (date.month + months - 1) / 12
month = (date.month + months - 1) % 12 + 1
day = date.day
last = monthrange(year, month)[1]
if day > last:
if endOfMonthBehaviour == 'RoundDown' or \
endOfMonthBehaviour == 'RoundOut' and months < 0 or \
endOfMonthBehaviour == 'RoundIn' and months > 0:
day = last
elif endOfMonthBehaviour == 'RoundUp' or \
endOfMonthBehaviour == 'RoundOut' and months > 0 or \
endOfMonthBehaviour == 'RoundIn' and months < 0:
# we don't need to worry about incrementing the year
# because there will never be a day in December > 31
month += 1
day = 1
return datetime.date(year, month, day)
>>> from calendar import monthrange
>>> import datetime
>>> add_months(datetime.datetime(2016, 1, 31), 1)
datetime.date(2016, 3, 1)
>>> add_months(datetime.datetime(2016, 1, 31), -2)
datetime.date(2015, 12, 1)
>>> add_months(datetime.datetime(2016, 1, 31), -2, 'RoundDown')
datetime.date(2015, 11, 30)
given that your datetime variable is called date:
date=datetime.datetime(year=date.year+int((date.month+6)/12),
month=(date.month+6)%13 + (1 if (date.month +
months>12) else 0), day=date.day)
General function to get next date after/before x months.
from datetime import date
def after_month(given_date, month):
yyyy = int(((given_date.year * 12 + given_date.month) + month)/12)
mm = int(((given_date.year * 12 + given_date.month) + month)%12)
if mm == 0:
yyyy -= 1
mm = 12
return given_date.replace(year=yyyy, month=mm)
if __name__ == "__main__":
today = date.today()
print(today)
for mm in [-12, -1, 0, 1, 2, 12, 20 ]:
next_date = after_month(today, mm)
print(next_date)
Im chiming in late, but
check out Ken Reitz Maya module,
https://github.com/kennethreitz/maya
something like this may help you, just change hours=1 to days=1 or years=1
>>> from maya import MayaInterval
# Create an event that is one hour long, starting now.
>>> event_start = maya.now()
>>> event_end = event_start.add(hours=1)
>>> event = MayaInterval(start=event_start, end=event_end)
The "python-dateutil" (external extension) is a good solution, but you can do it with build-in Python modules (datetime and datetime)
I made a short and simple code, to solve it (dealing with year, month and day)
(running: Python 3.8.2)
from datetime import datetime
from calendar import monthrange
# Time to increase (in months)
inc = 12
# Returns mod of the division for 12 (months)
month = ((datetime.now().month + inc) % 12) or 1
# Increase the division by 12 (months), if necessary (+ 12 months increase)
year = datetime.now().year + int((month + inc) / 12)
# (IF YOU DON'T NEED DAYS,CAN REMOVE THE BELOW CODE)
# Returns the same day in new month, or the maximum day of new month
day = min(datetime.now().day,monthrange(year, month)[1])
print("Year: {}, Month: {}, Day: {}".format(year, month, day))

Python datetime to decimal year: one day off, where is the bug?

I implemented a datetime.datetime to decimal year (year fraction) converter based on a previous answer. I am not getting the expected results but I can't find the bug.
from datetime import datetime, timedelta
def decimal_year_to_datetime(decimal_year_float):
year = int(decimal_year_float)
remain = decimal_year_float - year
base = datetime(year, 1, 1)
whole_year_time_delta = (base.replace(year=base.year + 1) - base)
fractional_seconds = whole_year_time_delta.total_seconds() * remain
our_time_delta = timedelta(seconds=fractional_seconds)
result = base + our_time_delta
return result
def test_conversion():
year = 2013
month = 1
day = 1
hour = 2
minute = 16
second = 48
date = datetime(year=year, month=month, day=day)
fraction_of_the_day = (hour + (minute + second / 60.0) / 60.0) / 24.
days_in_year = (date.replace(year=date.year + 1) - date).days
dec_yr = year + (date.timetuple().tm_yday +
fraction_of_the_day) / float(days_in_year)
expect_date = datetime(year=year, month=month, day=day,
hour=hour, minute=minute, second=second)
got_date = decimal_year_to_datetime(dec_yr)
assert(got_date == expect_date)
if __name__ == '__main__':
test_conversion()
I seem to be a day (an and a fraction of a second) off with my conversion. But I cannot see the bug.
Have I missed something obvious?
You're out by one day (a classic fencepost error!) because, although we consider 1st January to to be day one of the year, in computing terms that's the zeroth day. The simplest fix is to add in a - 1, but your approach seems generally quite complex; I would do it as follows:
def dt_to_dec(dt):
"""Convert a datetime to decimal year."""
year_start = datetime(dt.year, 1, 1)
year_end = year_start.replace(year=dt.year+1)
return dt.year + ((dt - year_start).total_seconds() / # seconds so far
float((year_end - year_start).total_seconds())) # seconds in year
In use:
>>> dec = dt_to_dec(datetime(2013, 1, 1, 2, 16, 48))
>>> dec
2013.0002602739726
>>> decimal_year_to_datetime(dec)
datetime.datetime(2013, 1, 1, 2, 16, 47, 999999)
(given floating point accuracy, that's as close as you're likely to get...)

Get (year,month) for the last X months

I got a very simple thing to to in python:
I need a list of tuples (year,month) for the last x months starting (and including) from today. So, for x=10 and today(July 2011), the command should output:
[(2011, 7), (2011, 6), (2011, 5), (2011, 4), (2011, 3),
(2011, 2), (2011, 1), (2010, 12), (2010, 11), (2010, 10)]
Only the default datetime implementation of python should be used. I came up with the following solution:
import datetime
[(d.year, d.month) for d in [datetime.date.today()-datetime.timedelta(weeks=4*i) for i in range(0,10)]]
This solution outputs the correct solution for my test cases but I'm not comfortable with this solution: It assumes that a month has four weeks and this is simply not true. I could replace the weeks=4 with days=30 which would make a better solution but it is still not correct.
The other solution which came to my mind is to use simple maths and subtract 1 from a months counter and if the month-counter is 0, subtract 1 from a year counter. The problem with this solution: It requires more code and isn't very readable either.
So how can this be done correctly?
I don't see it documented anywhere, but time.mktime will "roll over" into the correct year when given out-of-range, including negative, month values:
x = 10
now = time.localtime()
print([time.localtime(time.mktime((now.tm_year, now.tm_mon - n, 1, 0, 0, 0, 0, 0, 0)))[:2] for n in range(x)])
Using relativedelta ...
import datetime
from dateutil.relativedelta import relativedelta
def get_last_months(start_date, months):
for i in range(months):
yield (start_date.year,start_date.month)
start_date += relativedelta(months = -1)
>>> X = 10
>>> [i for i in get_last_months(datetime.datetime.today(), X)]
>>> [(2013, 2), (2013, 1), (2012, 12), (2012, 11), (2012, 10), (2012, 9), (2012, 8), (2012, 7), (2012, 6), (2012, 5)]
Neatest would be to use integer division (//) and modulus (%) functions, representing the month by the number of months since year 0:
months = year * 12 + month - 1 # Months since year 0 minus 1
tuples = [((months - i) // 12, (months - i) % 12 + 1) for i in range(10)]
The - 1 in the months expression is required to get the correct answer when we add 1 to the result of the modulus function later to get 1-indexing (i.e. months go from 1 to 12 rather than 0 to 11).
Or you might want to create a generator:
def year_month_tuples(year, month):
months = year * 12 + month - 1 # -1 to reflect 1-indexing
while True:
yield (months // 12, months % 12 + 1) # +1 to reflect 1-indexing
months -= 1 # next time we want the previous month
Which could be used as:
>>> tuples = year_month_tuples(2011, 7)
>>> [tuples.next() for i in range(10)]
Update: Adding a timedelta version anyway, as it looks prettier :)
def get_years_months(start_date, months):
for i in range(months):
yield (start_date.year, start_date.month)
start_date -= datetime.timedelta(days=calendar.monthrange(start_date.year, start_date.month)[1])
You don't need to work with timedelta since you only need year and month, which is fixed.
def get_years_months(my_date, num_months):
cur_month = my_date.month
cur_year = my_date.year
result = []
for i in range(num_months):
if cur_month == 0:
cur_month = 12
cur_year -= 1
result.append((cur_year, cur_month))
cur_month -= 1
return result
if __name__ == "__main__":
import datetime
result = get_years_months(datetime.date.today(), 10)
print result
If you create a function to do the date maths, it gets almost as nice as your original implementation:
def next_month(this_year, this_month):
if this_month == 0:
return (this_year - 1, 12)
else:
return (this_year, this_month - 1)
this_month = datetime.date.today().month()
this_year = datetime.date.today().year()
for m in range(0, 10):
yield (this_year, this_month)
this_year, this_month = next_month(this_year, this_month)
if you want to do it without datetime libraries, you can convert to months since year 0 and then convert back
end_year = 2014
end_month = 5
start_year = 2013
start_month = 7
print list = [(a/12,a % 12+1) for a in range(12*end_year+end_month-1,12*start_year+start_month-2,-1)]
python 3 (// instead of /):
list = [(a//12,a % 12+1) for a in range(12*end_year+end_month-1,12*start_year+start_month-2,-1)]
print(list)
[(2014, 5),
(2014, 4),
(2014, 3),
(2014, 2),
(2014, 1),
(2013, 12),
(2013, 11),
(2013, 10),
(2013, 9),
(2013, 8),
(2013, 7)]
Or you can define a function to get the last month, and then print the months (
it's a bit rudimentary)
def last_month(year_month):#format YYYY-MM
aux = year_month.split('-')
m = int(aux[1])
y = int(aux[0])
if m-1 == 0:
return str(y-1)+"-12"
else:
return str(y)+"-"+str(m-1)
def print_last_month(ran, year_month= str(datetime.datetime.today().year)+'-'+str(datetime.datetime.today().month)):
i = 1
if ran != 10:
print( last_month(year_month) )
print_last_month(i+1, year_month= last_month(year_month))
def list_last_year_month(self):
last_day_of_prev_month = date.today()
number_of_years = self.number_of_years
time_list = collections.defaultdict(list)
for y in range(number_of_years+1):
for m in range(13):
last_day_of_prev_month = last_day_of_prev_month.replace(day=1) - timedelta(days=1)
last_month = str(last_day_of_prev_month.month)
last_year = str(last_day_of_prev_month.year)
time_list[last_year].append(last_month)
return time_list
Simple solution using datetime and relativedelta functions. This returns the past dates by subtracting the number of months(input). This function will return the full date and using the below functions it is possible to get the year and month separately.
from datetime import date
from dateutil.relativedelta import relativedelta
def get_past_date(number_of_months):
return date.today() - relativedelta(months=number_of_months)
to get the year from the date
def get_year_from_the_date(date):
return date.year
to get the month from the date
def get_month_from_the_date(date):
return date.month

How do I calculate the date six months from the current date using the datetime Python module?

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 found this solution to be good. (This uses the python-dateutil extension)
from datetime import date
from dateutil.relativedelta import relativedelta
six_months = date.today() + relativedelta(months=+6)
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.)
$ date(2010,12,31)+relativedelta(months=+1)
datetime.date(2011, 1, 31)
$ date(2010,12,31)+relativedelta(months=+2)
datetime.date(2011, 2, 28)
Well, that depends what you mean by 6 months from the current date.
Using natural months:
inc = 6
year = year + (month + inc - 1) // 12
month = (month + inc - 1) % 12 + 1
Using a banker's definition, 6*30:
date += datetime.timedelta(6 * 30)
With Python 3.x you can do it like this:
from datetime import datetime, timedelta
from dateutil.relativedelta import *
date = datetime.now()
print(date)
# 2018-09-24 13:24:04.007620
date = date + relativedelta(months=+6)
print(date)
# 2019-03-24 13:24:04.007620
but you will need to install python-dateutil module:
pip install python-dateutil
So, here is an example of the dateutil.relativedelta which I found useful for iterating through the past year, skipping a month each time to the present date:
>>> import datetime
>>> from dateutil.relativedelta import relativedelta
>>> today = datetime.datetime.today()
>>> month_count = 0
>>> while month_count < 12:
... day = today - relativedelta(months=month_count)
... print day
... month_count += 1
...
2010-07-07 10:51:45.187968
2010-06-07 10:51:45.187968
2010-05-07 10:51:45.187968
2010-04-07 10:51:45.187968
2010-03-07 10:51:45.187968
2010-02-07 10:51:45.187968
2010-01-07 10:51:45.187968
2009-12-07 10:51:45.187968
2009-11-07 10:51:45.187968
2009-10-07 10:51:45.187968
2009-09-07 10:51:45.187968
2009-08-07 10:51:45.187968
As with the other answers, you have to figure out what you actually mean by "6 months from now." If you mean "today's day of the month in the month six years in the future" then this would do:
datetime.datetime.now() + relativedelta(months=6)
For beginning of month to month calculation:
from datetime import timedelta
from dateutil.relativedelta import relativedelta
end_date = start_date + relativedelta(months=delta_period) + timedelta(days=-delta_period)
Python can use datautil package for that, Please see the example below
It's not Just limited to that, you can pass combination of days, Months and Years at the same time also.
import datetime
from dateutil.relativedelta import relativedelta
# subtract months
proc_dt = datetime.date(2021,8,31)
proc_dt_minus_3_months = proc_dt + relativedelta(months=-3)
print(proc_dt_minus_3_months)
# add months
proc_dt = datetime.date(2021,8,31)
proc_dt_plus_3_months = proc_dt + relativedelta(months=+3)
print(proc_dt_plus_3_months)
# subtract days:
proc_dt = datetime.date(2021,8,31)
proc_dt_minus_3_days = proc_dt + relativedelta(days=-3)
print(proc_dt_minus_3_days)
# add days days:
proc_dt = datetime.date(2021,8,31)
proc_dt_plus_3_days = proc_dt + relativedelta(days=+3)
print(proc_dt_plus_3_days)
# subtract years:
proc_dt = datetime.date(2021,8,31)
proc_dt_minus_3_years = proc_dt + relativedelta(years=-3)
print(proc_dt_minus_3_years)
# add years:
proc_dt = datetime.date(2021,8,31)
proc_dt_plus_3_years = proc_dt + relativedelta(years=+3)
print(proc_dt_plus_3_years)
Results:
2021-05-31
2021-11-30
2021-08-28
2021-09-03
2018-08-31
2024-08-31
This solution works correctly for December, which most of the answers on this page do not.
You need to first shift the months from a 1-based index (ie Jan = 1) to a 0-based index (ie Jan = 0) before using modulus ( % ) or integer division ( // ), otherwise November (11) plus 1 month gives you 12, which when finding the remainder ( 12 % 12 ) gives 0.
(And dont suggest "(month % 12) + 1" or Oct + 1 = december!)
def AddMonths(d,x):
newmonth = ((( d.month - 1) + x ) % 12 ) + 1
newyear = int(d.year + ((( d.month - 1) + x ) / 12 ))
return datetime.date( newyear, newmonth, d.day)
However ... This doesnt account for problem like Jan 31 + one month. So we go back to the OP - what do you mean by adding a month? One solution is to backtrack until you get to a valid day, given that most people would presume the last day of jan, plus one month, equals the last day of Feb.
This will work on negative numbers of months too.
Proof:
>>> import datetime
>>> AddMonths(datetime.datetime(2010,8,25),1)
datetime.date(2010, 9, 25)
>>> AddMonths(datetime.datetime(2010,8,25),4)
datetime.date(2010, 12, 25)
>>> AddMonths(datetime.datetime(2010,8,25),5)
datetime.date(2011, 1, 25)
>>> AddMonths(datetime.datetime(2010,8,25),13)
datetime.date(2011, 9, 25)
>>> AddMonths(datetime.datetime(2010,8,25),24)
datetime.date(2012, 8, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-1)
datetime.date(2010, 7, 25)
>>> AddMonths(datetime.datetime(2010,8,25),0)
datetime.date(2010, 8, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-12)
datetime.date(2009, 8, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-8)
datetime.date(2009, 12, 25)
>>> AddMonths(datetime.datetime(2010,8,25),-7)
datetime.date(2010, 1, 25)>>>
What do you mean by "6 months"?
Is 2009-02-13 + 6 months == 2009-08-13? Or is it 2009-02-13 + 6*30 days?
import mx.DateTime as dt
#6 Months
dt.now()+dt.RelativeDateTime(months=6)
#result is '2009-08-13 16:28:00.84'
#6*30 days
dt.now()+dt.RelativeDateTime(days=30*6)
#result is '2009-08-12 16:30:03.35'
More info about mx.DateTime
This doesn't answer the specific question (using datetime only) but, given that others suggested the use of different modules, here there is a solution using pandas.
import datetime as dt
import pandas as pd
date = dt.date.today() - \
pd.offsets.DateOffset(months=6)
print(date)
2019-05-04 00:00:00
Which works as expected in leap years
date = dt.datetime(2019,8,29) - \
pd.offsets.DateOffset(months=6)
print(date)
2019-02-28 00:00:00
There's no direct way to do it with Python's datetime.
Check out the relativedelta type at python-dateutil. It allows you to specify a time delta in months.
I know this was for 6 months, however the answer shows in google for "adding months in python" if you are adding one month:
import calendar
date = datetime.date.today() //Or your date
datetime.timedelta(days=calendar.monthrange(date.year,date.month)[1])
this would count the days in the current month and add them to the current date, using 365/12 would ad 1/12 of a year can causes issues for short / long months if your iterating over the date.
Just use the timetuple method to extract the months, add your months and build a new dateobject. If there is a already existing method for this I do not know it.
import datetime
def in_the_future(months=1):
year, month, day = datetime.date.today().timetuple()[:3]
new_month = month + months
return datetime.date(year + (new_month / 12), (new_month % 12) or 12, day)
The API is a bit clumsy, but works as an example. Will also obviously not work on corner-cases like 2008-01-31 + 1 month. :)
Using Python standard libraries, i.e. without dateutil or others, and solving the 'February 31st' problem:
import datetime
import calendar
def add_months(date, months):
months_count = date.month + months
# Calculate the year
year = date.year + int(months_count / 12)
# Calculate the month
month = (months_count % 12)
if month == 0:
month = 12
# Calculate the day
day = date.day
last_day_of_month = calendar.monthrange(year, month)[1]
if day > last_day_of_month:
day = last_day_of_month
new_date = datetime.date(year, month, day)
return new_date
Testing:
>>>date = datetime.date(2018, 11, 30)
>>>print(date, add_months(date, 3))
(datetime.date(2018, 11, 30), datetime.date(2019, 2, 28))
>>>print(date, add_months(date, 14))
(datetime.date(2018, 12, 31), datetime.date(2020, 2, 29))
Dateutil package has implementation of such functionality. But be aware, that this will be naive, as others pointed already.
I have a better way to solve the 'February 31st' problem:
def add_months(start_date, months):
import calendar
year = start_date.year + (months / 12)
month = start_date.month + (months % 12)
day = start_date.day
if month > 12:
month = month % 12
year = year + 1
days_next = calendar.monthrange(year, month)[1]
if day > days_next:
day = days_next
return start_date.replace(year, month, day)
I think that it also works with negative numbers (to subtract months), but I haven't tested this very much.
A quick suggestion is Arrow
pip install arrow
>>> import arrow
>>> arrow.now().date()
datetime.date(2019, 6, 28)
>>> arrow.now().shift(months=6).date()
datetime.date(2019, 12, 28)
The QDate class of PyQt4 has an addmonths function.
>>>from PyQt4.QtCore import QDate
>>>dt = QDate(2009,12,31)
>>>required = dt.addMonths(6)
>>>required
PyQt4.QtCore.QDate(2010, 6, 30)
>>>required.toPyDate()
datetime.date(2010, 6, 30)
Modified the AddMonths() for use in Zope and handling invalid day numbers:
def AddMonths(d,x):
days_of_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
newmonth = ((( d.month() - 1) + x ) % 12 ) + 1
newyear = d.year() + ((( d.month() - 1) + x ) // 12 )
if d.day() > days_of_month[newmonth-1]:
newday = days_of_month[newmonth-1]
else:
newday = d.day()
return DateTime( newyear, newmonth, newday)
import time
def add_month(start_time, months):
ret = time.strptime(start_time, '%Y-%m-%d')
t = list(ret)
t[1] += months
if t[1] > 12:
t[0] += 1 + int(months / 12)
t[1] %= 12
return int(time.mktime(tuple(t)))
Modified Johannes Wei's answer in the case 1new_month = 121. This works perfectly for me. The months could be positive or negative.
def addMonth(d,months=1):
year, month, day = d.timetuple()[:3]
new_month = month + months
return datetime.date(year + ((new_month-1) / 12), (new_month-1) % 12 +1, day)
How about this? Not using another library (dateutil) or timedelta?
building on vartec's answer I did this and I believe it works:
import datetime
today = datetime.date.today()
six_months_from_today = datetime.date(today.year + (today.month + 6)/12, (today.month + 6) % 12, today.day)
I tried using timedelta, but because it is counting the days, 365/2 or 6*356/12 does not always translate to 6 months, but rather 182 days. e.g.
day = datetime.date(2015, 3, 10)
print day
>>> 2015-03-10
print (day + datetime.timedelta(6*365/12))
>>> 2015-09-08
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, Not 2015-09-08)
I hope you find this helpful.
import datetime
'''
Created on 2011-03-09
#author: tonydiep
'''
def add_business_months(start_date, months_to_add):
"""
Add months in the way business people think of months.
Jan 31, 2011 + 1 month = Feb 28, 2011 to business people
Method: Add the number of months, roll back the date until it becomes a valid date
"""
# determine year
years_change = months_to_add / 12
# determine if there is carryover from adding months
if (start_date.month + (months_to_add % 12) > 12 ):
years_change = years_change + 1
new_year = start_date.year + years_change
# determine month
work = months_to_add % 12
if 0 == work:
new_month = start_date.month
else:
new_month = (start_date.month + (work % 12)) % 12
if 0 == new_month:
new_month = 12
# determine day of the month
new_day = start_date.day
if(new_day in [31, 30, 29, 28]):
#user means end of the month
new_day = 31
new_date = None
while (None == new_date and 27 < new_day):
try:
new_date = start_date.replace(year=new_year, month=new_month, day=new_day)
except:
new_day = new_day - 1 #wind down until we get to a valid date
return new_date
if __name__ == '__main__':
#tests
dates = [datetime.date(2011, 1, 31),
datetime.date(2011, 2, 28),
datetime.date(2011, 3, 28),
datetime.date(2011, 4, 28),
datetime.date(2011, 5, 28),
datetime.date(2011, 6, 28),
datetime.date(2011, 7, 28),
datetime.date(2011, 8, 28),
datetime.date(2011, 9, 28),
datetime.date(2011, 10, 28),
datetime.date(2011, 11, 28),
datetime.date(2011, 12, 28),
]
months = range(1, 24)
for start_date in dates:
for m in months:
end_date = add_business_months(start_date, m)
print("%s\t%s\t%s" %(start_date, end_date, m))
Rework of an earlier answer by user417751. Maybe not so pythonic way, but it takes care of different month lengths and leap years. In this case 31 January 2012 + 1 month = 29 February 2012.
import datetime
import calendar
def add_mths(d, x):
newday = d.day
newmonth = (((d.month - 1) + x) % 12) + 1
newyear = d.year + (((d.month - 1) + x) // 12)
if newday > calendar.mdays[newmonth]:
newday = calendar.mdays[newmonth]
if newyear % 4 == 0 and newmonth == 2:
newday += 1
return datetime.date(newyear, newmonth, newday)
Yet another solution - hope someone will like it:
def add_months(d, months):
return d.replace(year=d.year+months//12).replace(month=(d.month+months)%12)
This solution doesn't work for days 29,30,31 for all cases, so more robust solution is needed (which is not so nice anymore :) ):
def add_months(d, months):
for i in range(4):
day = d.day - i
try:
return d.replace(day=day).replace(year=d.year+int(months)//12).replace(month=(d.month+int(months))%12)
except:
pass
raise Exception("should not happen")
From this answer, see parsedatetime. Code example follows. More details: unit test with many natural-language -> YYYY-MM-DD conversion examples, and apparent parsedatetime conversion challenges/bugs.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time, calendar
from datetime import date
# from https://github.com/bear/parsedatetime
import parsedatetime as pdt
def print_todays_date():
todays_day_of_week = calendar.day_name[date.today().weekday()]
print "today's date = " + todays_day_of_week + ', ' + \
time.strftime('%Y-%m-%d')
def convert_date(natural_language_date):
cal = pdt.Calendar()
(struct_time_date, success) = cal.parse(natural_language_date)
if success:
formal_date = time.strftime('%Y-%m-%d', struct_time_date)
else:
formal_date = '(conversion failed)'
print '{0:12s} -> {1:10s}'.format(natural_language_date, formal_date)
print_todays_date()
convert_date('6 months')
The above code generates the following from a MacOSX machine:
$ ./parsedatetime_simple.py
today's date = Wednesday, 2015-05-13
6 months -> 2015-11-13
$
Here's a example which allows the user to decide how to return a date where the day is greater than the number of days in the month.
def add_months(date, months, endOfMonthBehaviour='RoundUp'):
assert endOfMonthBehaviour in ['RoundDown', 'RoundIn', 'RoundOut', 'RoundUp'], \
'Unknown end of month behaviour'
year = date.year + (date.month + months - 1) / 12
month = (date.month + months - 1) % 12 + 1
day = date.day
last = monthrange(year, month)[1]
if day > last:
if endOfMonthBehaviour == 'RoundDown' or \
endOfMonthBehaviour == 'RoundOut' and months < 0 or \
endOfMonthBehaviour == 'RoundIn' and months > 0:
day = last
elif endOfMonthBehaviour == 'RoundUp' or \
endOfMonthBehaviour == 'RoundOut' and months > 0 or \
endOfMonthBehaviour == 'RoundIn' and months < 0:
# we don't need to worry about incrementing the year
# because there will never be a day in December > 31
month += 1
day = 1
return datetime.date(year, month, day)
>>> from calendar import monthrange
>>> import datetime
>>> add_months(datetime.datetime(2016, 1, 31), 1)
datetime.date(2016, 3, 1)
>>> add_months(datetime.datetime(2016, 1, 31), -2)
datetime.date(2015, 12, 1)
>>> add_months(datetime.datetime(2016, 1, 31), -2, 'RoundDown')
datetime.date(2015, 11, 30)
given that your datetime variable is called date:
date=datetime.datetime(year=date.year+int((date.month+6)/12),
month=(date.month+6)%13 + (1 if (date.month +
months>12) else 0), day=date.day)
General function to get next date after/before x months.
from datetime import date
def after_month(given_date, month):
yyyy = int(((given_date.year * 12 + given_date.month) + month)/12)
mm = int(((given_date.year * 12 + given_date.month) + month)%12)
if mm == 0:
yyyy -= 1
mm = 12
return given_date.replace(year=yyyy, month=mm)
if __name__ == "__main__":
today = date.today()
print(today)
for mm in [-12, -1, 0, 1, 2, 12, 20 ]:
next_date = after_month(today, mm)
print(next_date)
Im chiming in late, but
check out Ken Reitz Maya module,
https://github.com/kennethreitz/maya
something like this may help you, just change hours=1 to days=1 or years=1
>>> from maya import MayaInterval
# Create an event that is one hour long, starting now.
>>> event_start = maya.now()
>>> event_end = event_start.add(hours=1)
>>> event = MayaInterval(start=event_start, end=event_end)
The "python-dateutil" (external extension) is a good solution, but you can do it with build-in Python modules (datetime and datetime)
I made a short and simple code, to solve it (dealing with year, month and day)
(running: Python 3.8.2)
from datetime import datetime
from calendar import monthrange
# Time to increase (in months)
inc = 12
# Returns mod of the division for 12 (months)
month = ((datetime.now().month + inc) % 12) or 1
# Increase the division by 12 (months), if necessary (+ 12 months increase)
year = datetime.now().year + int((month + inc) / 12)
# (IF YOU DON'T NEED DAYS,CAN REMOVE THE BELOW CODE)
# Returns the same day in new month, or the maximum day of new month
day = min(datetime.now().day,monthrange(year, month)[1])
print("Year: {}, Month: {}, Day: {}".format(year, month, day))

Categories

Resources