Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, December 29, 2015

How to iterate over a timespan after days, hours, weeks and months in Python?

How to iterate over a timespan after days, hours, weeks and months in Python?


How do I iterate over a timespan after days, hours, weeks or months?

Something like:

for date in foo(from_date, to_date, delta=HOURS):      print date  

Where foo is a function, returning an iterator. I've been looking at the calendar module, but that only works for one specific year or month, not between dates.

Answer by dguaraglia for How to iterate over a timespan after days, hours, weeks and months in Python?


This library provides a handy calendar tool: mxDateTime, that should be enough :)

Answer by DzinX for How to iterate over a timespan after days, hours, weeks and months in Python?


I don't think there is a method in Python library, but you can easily create one yourself using datetime module:

from datetime import date, datetime, timedelta    def datespan(startDate, endDate, delta=timedelta(days=1)):      currentDate = startDate      while currentDate < endDate:          yield currentDate          currentDate += delta  

Then you could use it like this:

>>> for day in datespan(date(2007, 3, 30), date(2007, 4, 3),   >>>                     delta=timedelta(days=1)):  >>>     print day  2007-03-30  2007-03-31  2007-04-01  2007-04-02  

Or, if you wish to make your delta smaller:

>>> for timestamp in datespan(datetime(2007, 3, 30, 15, 30),   >>>                           datetime(2007, 3, 30, 18, 35),   >>>                           delta=timedelta(hours=1)):  >>>     print timestamp  2007-03-30 15:30:00  2007-03-30 16:30:00  2007-03-30 17:30:00  2007-03-30 18:30:00  

Answer by giltay for How to iterate over a timespan after days, hours, weeks and months in Python?


For iterating over months you need a different recipe, since timedeltas can't express "one month".

from datetime import date    def jump_by_month(start_date, end_date, month_step=1):      current_date = start_date      while current_date < end_date:          yield current_date          carry, new_month = divmod(current_date.month - 1 + month_step, 12)          new_month += 1          current_date = current_date.replace(year=current_date.year + carry,                                              month=new_month)  

(NB: you have to subtract 1 from the month for the modulus operation then add it back to new_month, since months in datetime.dates start at 1.)

Answer by Thomas Vander Stichele for How to iterate over a timespan after days, hours, weeks and months in Python?


Use dateutil and its rrule implementation, like so:

from dateutil import rrule  from datetime import datetime, timedelta    now = datetime.now()  hundredDaysLater = now + timedelta(days=100)    for dt in rrule.rrule(rrule.MONTHLY, dtstart=now, until=hundredDaysLater):      print dt  

Output is

2008-09-30 23:29:54  2008-10-30 23:29:54  2008-11-30 23:29:54  2008-12-30 23:29:54  

Replace MONTHLY with any of YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, or SECONDLY. Replace dtstart and until with whatever datetime object you want.

This recipe has the advantage for working in all cases, including MONTHLY. Only caveat I could find is that if you pass a day number that doesn't exist for all months, it skips those months.

Answer by name1 for How to iterate over a timespan after days, hours, weeks and months in Python?


You should modify this line to make this work correctly:

current_date = current_date.replace(year=current_date.year + carry,month=new_month,day=1)

;)

Answer by user4789833 for How to iterate over a timespan after days, hours, weeks and months in Python?


I used the above program to calculate the 28 day cycle for rent payment , but my question is what if i paid a few days late every month and I have to calculate the total of all those days i owe rent and print it?


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.