2008-09-30 106 views
46

如何在几天,几小时,几周或几个月后迭代时间跨度?如何在Python中的几天,几小时,几周和几个月后迭代时间跨度?

喜欢的东西:

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

其中foo是一个函数,返回一个迭代器。我一直在看日历模块,但只适用于特定的一年或一个月,而不是日期之间。

+0

或者,您是否想要带有上述签名的功能? – 2008-09-30 15:56:00

回答

75

使用dateutil及其RRULE实施,像这样:

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 

输出是

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

用MONEYLY,MONTHLY,WEEKLY,DAILY,HOURLY,MINUTELY或SECONDLY中的任何一个替换MONTHLY。将dtstart替换为你想要的任何datetime对象。

这个配方在所有情况下都有优势,包括MONTHLY。唯一需要注意的是,如果你通过一个不存在所有月份的日数,它会跳过那几个月。

38

我不认为这是在Python库的方法,但你可以轻松地创建使用datetime模块自己一:

from datetime import date, datetime, timedelta 

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

然后,你可以使用这样的:

>>> 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 

或者,如果你想使你的小三角:

>>> 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 
+1

你不能指定一个月的timedelta – dangonfast 2014-08-06 12:02:14

6

对于迭代数月,您需要不同的配方,因为timedeltas不能表达“一个月”。

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) 

(注:你必须从一个月减1为模运算,然后在datetime.date S启动在1加回new_month,因为个月)

+0

有两件事: - 如果你设置start_date的日期为每个月不存在的数字,此代码会引发`ValueError` - 可能正是因为这个原因,没有月份-timedelta;我建议使用`timedelta(days = 30)`作为一个很好的近似值。 – DzinX 2008-09-30 17:29:24

+0

好的。然而,有时候,近似值不够好(我的“支付租金”提醒*在每个月的第一天时间为6点30分)。配方可以修改 - 可能有一些额外的状态 - 以提供理智的功能,不管是什么。 – giltay 2008-10-02 14:53:24

-2

您应修改这一行,使这项工作正确:

CURRENT_DATE = current_date.replace(年= current_date.year +携带,一个月= new_month,天= 1)

;)

0

月迭代方法:

def months_between(date_start, date_end): 
    months = [] 

    # Make sure start_date is smaller than end_date 
    if date_start > date_end: 
     tmp = date_start 
     date_start = date_end 
     date_end = tmp 

    tmp_date = date_start 
    while tmp_date.month <= date_end.month or tmp_date.year < date_end.year: 
     months.append(tmp_date) # Here you could do for example: months.append(datetime.datetime.strftime(tmp_date, "%b '%y")) 

     if tmp_date.month == 12: # New year 
      tmp_date = datetime.date(tmp_date.year + 1, 1, 1) 
     else: 
      tmp_date = datetime.date(tmp_date.year, tmp_date.month + 1, 1) 
    return months 

更多的代码,但它会做得很好的交易,随着时间检查长期给定日期是为了...

相关问题