I am trying to generate list of months between two dates. For Example:
startDate = '2016-1-31'
endDate = '2017-3-26'
It should result as:
datetime.date(2016, 1, 31)
datetime.date(2016, 2, 28)
and so on....
I am trying like this
startDate = '2016-1-28'
endDate = '2017-3-26'
start = date(*map(int, startDate.split('-')))
end = date(*map(int, endDate.split('-')))
week = start
dateData = []
while week <= end:
dateData.append(week)
week = week + datetime.timedelta(weeks=4)
pprint(dateData)
This gives result as:
[datetime.date(2016, 1, 31),
datetime.date(2016, 2, 28),
datetime.date(2016, 3, 27),
datetime.date(2016, 4, 24),
datetime.date(2016, 5, 22),
datetime.date(2016, 6, 19),
datetime.date(2016, 7, 17),
datetime.date(2016, 8, 14),
datetime.date(2016, 9, 11),
datetime.date(2016, 10, 9),
datetime.date(2016, 11, 6),
datetime.date(2016, 12, 4),
datetime.date(2017, 1, 1),
datetime.date(2017, 1, 29),
datetime.date(2017, 2, 26),
datetime.date(2017, 3, 26)]
Here "2016, 12" & "2017, 1" is repeating twice. Can anybody help me solve this problem.
Example: Oracle MONTHS_BETWEEN () function The following statement calculates the months between two specified dates: SQL> SELECT MONTHS_BETWEEN 2 (TO_DATE('02-02-2015','MM-DD-YYYY'), 3 TO_DATE('12-01-2014','MM-DD-YYYY') ) "Months" 4 FROM DUAL;.
As below screenshot shown, supposing you need to compare two date lists and match the dates by month and year only, you can apply the below formula to achieve it. 1. Select a blank cell, enter formula =MONTH(A2)&YEAR(A2)=MONTH(B2)&YEAR(B2) into the Formula Bar, and then press the Enter key.
Another method to get the number of months between two specified dates is by using the YEARFRAC function. The YEARFRAC function will take a start date and end date as input arguments and it will give you the number of years that have passed during these two dates.
You could use the dateutil
extension's relativedelta
method like below -
from datetime import datetime
from dateutil.relativedelta import relativedelta
startDate = '2016-1-28'
endDate = '2017-3-26'
cur_date = start = datetime.strptime(startDate, '%Y-%m-%d').date()
end = datetime.strptime(endDate, '%Y-%m-%d').date()
while cur_date < end:
print(cur_date)
cur_date += relativedelta(months=1)
Following is the output
2016-01-28
2016-02-28
2016-03-28
2016-04-28
2016-05-28
2016-06-28
2016-07-28
2016-08-28
2016-09-28
2016-10-28
2016-11-28
2016-12-28
2017-01-28
2017-02-28
You can also use only pandas in one line:
import pandas as pd
pd.date_range('2018-01', '2020-05', freq='M')
The output will be:
DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31', '2018-04-30',
'2018-05-31', '2018-06-30', '2018-07-31', '2018-08-31',
'2018-09-30', '2018-10-31', '2018-11-30', '2018-12-31',
'2019-01-31', '2019-02-28', '2019-03-31', '2019-04-30',
'2019-05-31', '2019-06-30', '2019-07-31', '2019-08-31',
'2019-09-30', '2019-10-31', '2019-11-30', '2019-12-31',
'2020-01-31', '2020-02-29', '2020-03-31', '2020-04-30'],
dtype='datetime64[ns]', freq='M')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With