1

I have two datetime objects, let's say start_date and end_date. What I want to do is to make a list of datetime objects.

>>> from datetime import datetime
>>> from dateutil.rrule import rrule, MONTHLY
>>> start_date = datetime(2018, 9, 6, 0,)
>>> end_date = datetime(2018, 11, 26, 23, 59, 59)
>>> list(rrule(MONTHLY, dtstart=start_date, until=end_date))
[datetime.datetime(2018, 9, 6, 0, 0), datetime.datetime(2018, 10, 6, 0, 0), datetime.datetime(2018, 11, 6, 0, 0)]

I can do this with rrule(), it's moving from date to date but I want it to go month-wise and also include the end_date,

[
    datetime.datetime(2018,9,6,0,0), 
    datetime.datetime(2018,9,30,23,59),
    datetime.datetime(2018,10,1,0,0),
    datetime.datetime(2018,10,31,23,59),
    datetime.datetime(2018,11,1,0,0),
    datetime.datetime(2018,11,26,23,59)
]

I would prefer to do this without using pandas or numpy.

Sadan A.
  • 1,017
  • 1
  • 10
  • 28
  • 1
    Haven't you already got that as the result in your first code block... I'm not clear what that second code block showing a list of datetimes is for here? – Jon Clements Nov 26 '18 at 14:30
  • @JonClements I guess the OP is looking for a list with start_date and end_date for every month. I do agree with you that the question is not very clear. – rpanai Nov 26 '18 at 14:32
  • I guess that you can figure out a solution using [this](https://stackoverflow.com/questions/42950/get-last-day-of-the-month-in-python) – rpanai Nov 26 '18 at 14:35
  • @JonClements I want to go from start_date to end_date month by month, not a 30 days jump. – Sadan A. Nov 26 '18 at 15:28

2 Answers2

0

here's how you can get a list of dates between two ranges. you can modify it to add certain dates.

from datetime import datetime
from datetime import timedelta
import calendar
dates = []

start_date = datetime(2018, 9, 6, 0,)
end_date = datetime(2018, 10, 10, 0,)
index_date = start_date
while True:
    index_date = index_date
    dates.append(index_date)
    dayy = calendar.monthrange(index_date.year,index_date.month)
    index_date = index_date.replace(day= dayy[1])
    if  index_date > end_date:
        break
    dates.append(index_date)
    index_date = index_date + timedelta(days=1)
    if  index_date > end_date:
        break
print dates
Franco Pettigrosso
  • 4,056
  • 2
  • 20
  • 32
0

Try this

import datetime

def last_day_of_month(date):
    if date.month == 12:
        return date.replace(day=31)
    return date.replace(month=date.month+1, day=1) - datetime.timedelta(days=1)


def desired_output(start_date,end_date):
    curr_date = start_date
    desired_list = []
    desired_list.append(start_date)
    while curr_date<=end_date:
        if last_day_of_month(curr_date)>end_date:
            desired_list.append(end_date)
            break
        desired_list.append(last_day_of_month(curr_date))
        curr_date = last_day_of_month(curr_date) + datetime.timedelta(1)
        if curr_date<end_date:
            desired_list.append(curr_date)
        else:
            break
    return desired_list

print(desired_output(datetime.datetime(2018,9,6,0,0), datetime.datetime(2018,12,26,0,0)))
Arpit Singh
  • 398
  • 4
  • 11