3

Let's say I have a starting date of datetime(2007, 2, 15).

I want to step this date in a loop so that it's advanced to the 1st and 15th of each month.

So datetime(2007, 2, 15) would step to datetime(2007, 3, 1).

In the next iteration, it would step to datetime(2007, 3, 15)... then to datetime(2007, 4, 1) and so forth.

Is there any possible way to do this with timedelta or dateutils considering that, the number of days it has to step by, continuously changes?

3 Answers 3

2
from datetime import datetime
for m in range(1, 13):
    for d in (1, 15):
        print str(datetime(2013, m, d))

2013-01-01 00:00:00
2013-01-15 00:00:00
2013-02-01 00:00:00
2013-02-15 00:00:00
2013-03-01 00:00:00
2013-03-15 00:00:00
2013-04-01 00:00:00
2013-04-15 00:00:00
2013-05-01 00:00:00
2013-05-15 00:00:00
2013-06-01 00:00:00
2013-06-15 00:00:00
2013-07-01 00:00:00
2013-07-15 00:00:00
2013-08-01 00:00:00
2013-08-15 00:00:00
2013-09-01 00:00:00
2013-09-15 00:00:00
2013-10-01 00:00:00
2013-10-15 00:00:00
2013-11-01 00:00:00
2013-11-15 00:00:00
2013-12-01 00:00:00
2013-12-15 00:00:00

I tend to work with datetime more than date objects, but you could use datetime.date depending on your needs.

Sign up to request clarification or add additional context in comments.

Comments

1

I'd iterate through each day and ignore any date where the day of month isn't 1 or 15. Example:

import datetime

current_time = datetime.datetime(2007,2,15)
end_time = datetime.datetime(2008,4,1)

while current_time <= end_time:
  if current_time.day in [1,15]:
    print(current_time)
  current_time += datetime.timedelta(days=1)

This way you can iterate across multiple years and start on the 15th, both of which would be problematic with doog's solution.

Comments

0
from datetime import datetime    
d = datetime(month=2,year=2007,day=15)    
current_day = next_day = d.day
current_month = next_month = d.month
current_year = next_year = d.year   
for i in range(25):
    if current_day == 1:
        next_day = 15
    elif current_day == 15:
        next_day = 1
        if current_month == 12:
            next_month = 1
            next_year+=1
        else:
            next_month+=1    
    new_date=datetime(month=next_month,year=next_year,day=next_day)
    print new_date
    current_day,current_month,current_year=next_day,next_month,next_year

2007-03-01 00:00:00
2007-03-15 00:00:00
2007-04-01 00:00:00
2007-04-15 00:00:00
2007-05-01 00:00:00
2007-05-15 00:00:00
2007-06-01 00:00:00
2007-06-15 00:00:00
2007-07-01 00:00:00
2007-07-15 00:00:00
2007-08-01 00:00:00
2007-08-15 00:00:00
2007-09-01 00:00:00
2007-09-15 00:00:00
2007-10-01 00:00:00
2007-10-15 00:00:00
2007-11-01 00:00:00
2007-11-15 00:00:00
2007-12-01 00:00:00
2007-12-15 00:00:00
2008-01-01 00:00:00
2008-01-15 00:00:00
2008-02-01 00:00:00
2008-02-15 00:00:00
2008-03-01 00:00:00

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.