0

I have a starting date (lets say January 5th 2014) and an end date (lets say July 10th 2014). I know that an event occurs every Wednesday. Is there an easy way in Python to output all Wednesday with date between those date ranges?

So assuming January 7th is a Wednesday, then the code snippet would output 01.07.2014, 01.14.2014, 01.21.2014, and so on.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3607973
  • 345
  • 1
  • 5
  • 15
  • 1
    No built-in function as such, but you can easily add `timedelta(days=7)` to skip a week. – jonrsharpe Oct 06 '14 at 20:56
  • The question marked as "duplicate" misses half of the question here. – Mark Ransom Oct 06 '14 at 21:02
  • 1
    @MarkRansom: *half*? There is no attempt by the OP here, so we can make assumptions about how much they already can do themselves. Finding the next wednesday from a given date is also a dupe. Once you have both those pieces it is trivial to put this together. – Martijn Pieters Oct 06 '14 at 21:03
  • 1
    *Still* a duplicate of [Generate a list of datetimes between an interval in python](http://stackoverflow.com/q/10688006) The other 'half' is [Python: Find the date for the first Monday after a given a date](http://stackoverflow.com/q/6558535) – Martijn Pieters Oct 06 '14 at 21:04
  • @MartijnPieters The post your referenced as duplicate actually seems to do exactly what I was looking for, except for finding a specific week day from a given date, but I suppose I can find that. Thanks! I just didn't know how to word the question correctly, therefore didn't find any solutions. – user3607973 Oct 06 '14 at 21:06
  • Well. Now its all answered. Thanks! – user3607973 Oct 06 '14 at 21:07

1 Answers1

1

To get the first specific weekday after a given date, just add a timedelta of the difference in the weekdays:

wed = 2 # from the datetime.weekday definition
first_wed = start + datetime.timedelta(days=(7 + wed - start.weekday()) % 7)

Once you have that, please see Generate a list of datetimes between an interval in python.

Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • That part is a dupe of [Python: Find the date for the first Monday after a given a date](http://stackoverflow.com/q/6558535). Really, there was no need to reopen this for that nugget. – Martijn Pieters Oct 06 '14 at 21:06
  • Hrm, not sure that that deserved a downvote though (didn't vote). – Martijn Pieters Oct 06 '14 at 21:07
  • @MartijnPieters, you're more familiar with these questions than I am I guess. I have no problem closing this question now. – Mark Ransom Oct 06 '14 at 21:07