[Tutor] confused with dates and which way to start
johnf
jfabiani at yolo.com
Thu Feb 19 00:55:05 CET 2009
On Wednesday 18 February 2009 03:10:41 pm johnf 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???
> And is there something already written I should be using???? Also there is
> something called 'mx'.
>
> Thanks in advance.
Kent Johnson provided the following:
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
thanks Kent. This appears to work. I was not aware of datetime.timedelta().
I really think there should be a place to find this type of information.
I'll study the module. Again, thanks.
--
John Fabiani
More information about the Tutor
mailing list