[Tutor] confused with dates and which way to start

Che M pine508 at hotmail.com
Thu Feb 19 08:24:36 CET 2009




> From: jfabiani at yolo.com
> To: tutor at python.org
> Date: Wed, 18 Feb 2009 15:55:05 -0800
> Subject: Re: [Tutor] confused with dates and which way to start
> 
> 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.

Well, if you Google "dates Python", three page-downs down the first hit and it shows about datetime.timedelta().  It's also mentioned in the second hit.  Of course, it does require scanning and reading, since you start unsure of what to be on the lookout for.  

I'd also recommend you look at dateutil, because it is expressly for doing what you need, in case you need to do more and more complex date work.  The page includes a ton of example code:
http://labix.org/python-dateutil

_________________________________________________________________
Stay up to date on your PC, the Web, and your mobile phone with Windows Live.
http://clk.atdmt.com/MRT/go/msnnkwxp1020093185mrt/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090219/223b472c/attachment.htm>


More information about the Tutor mailing list