[Tutor] confused with dates and which way to start

Kent Johnson kent37 at tds.net
Thu Feb 19 00:35:19 CET 2009


On Wed, Feb 18, 2009 at 6:10 PM, johnf <jfabiani at yolo.com> wrote:
> Hi,
>
> I need to develope a routine that will provide the some dates between a
> starting date and an ending date.  The dates will be used to schedule
> instructors for specific class dates from the list of available dates.
> Class is normally on Monday, Wedesday, and Friday (or any days of the week
> including Sat and Sun).  If the length of the class is 6 months (01/01/09 -
> 06/30/09).  I need to generate all the dates that would match the Monday,
> Wedesday, and Friday (or whatever days are used for the class) for the entire
> period.
>
> I have done this is FoxPro in the past.  Now I'm in python 2.5.
>
> Now the problem is there appears many ways to deal with dates in python.
> time(), calendar(), datetime(), dateutil().  Which is the easy way guys???

This is pretty easy using datetime.date and timedelta. For example,
this shows all the M, W, F between (more or less) your given dates:

In [1]: from datetime import date, timedelta
In [3]: end = date(2009, 6, 30)
In [4]: start = date(2009, 1, 4) # a Sunday
In [7]: offsets = [timedelta(days=offset) for offset in (1, 3, 5) ] #
Mon, Wed, Fri
In [8]: current = start

In [9]: while current < end:
   ...:     for offset in offsets:
   ...:         print current + offset
   ...:     current += timedelta(days=7)

2009-01-05
2009-01-07
2009-01-09
2009-01-12
2009-01-14
2009-01-16
...etc

Kent


More information about the Tutor mailing list