Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- day_count = (end_date - start_date).days + 1
- for single_date in [d for d in (start_date + timedelta(n) for n in range(day_count)) if d <= end_date]:
- print strftime("%Y-%m-%d", single_date.timetuple())
- 2009-05-30
- 2009-05-31
- 2009-06-01
- 2009-06-02
- 2009-06-03
- 2009-06-04
- 2009-06-05
- 2009-06-06
- 2009-06-07
- 2009-06-08
- 2009-06-09
- for single_date in (start_date + timedelta(n) for n in range(day_count)):
- print ...
- def daterange(start_date, end_date):
- for n in range((end_date - start_date).days):
- yield start_date + timedelta(n)
- for single_date in daterange(start_date, end_date):
- print strftime("%Y-%m-%d", single_date.timetuple())
- d = start_date
- delta = datetime.timedelta(days=1)
- while d <= end_date:
- print d.strftime("%Y-%m-%d")
- d += delta
- from datetime import date
- from dateutil.rrule import rrule, DAILY
- a = date(2009, 5, 30)
- b = date(2009, 6, 9)
- for dt in rrule(DAILY, dtstart=a, until=b):
- print dt.strftime("%Y-%m-%d")
- import datetime
- def daterange(start, stop, step=datetime.timedelta(days=1), inclusive=False):
- # inclusive=False to behave like range by default
- if step.days > 0:
- while start < stop:
- yield start
- start = start + step
- # not +=! don't modify object passed in if it's mutable
- # since this function is not restricted to
- # only types from datetime module
- elif step.days < 0:
- while start > stop:
- yield start
- start = start + step
- if inclusive and start == stop:
- yield start
- # ...
- for date in daterange(start_date, end_date, inclusive=True):
- print strftime("%Y-%m-%d", date.timetuple())
- from dateutil.rrule import rrule, DAILY
- rr = rrule(DAILY, dtstart=start_date, until=end_date)
- print list(rr)
- import datetime
- def daterange(start, stop, step_days=1):
- current = start
- step = datetime.timedelta(step_days)
- if step_days > 0:
- while current < stop:
- yield current
- current += step
- elif step_days < 0:
- while current > stop:
- yield current
- current += step
- else:
- raise ValueError("daterange() step_days argument must not be zero")
- if __name__ == "__main__":
- from pprint import pprint as pp
- lo = datetime.date(2008, 12, 27)
- hi = datetime.date(2009, 1, 5)
- pp(list(daterange(lo, hi)))
- pp(list(daterange(hi, lo, -1)))
- pp(list(daterange(lo, hi, 7)))
- pp(list(daterange(hi, lo, -7)))
- assert not list(daterange(lo, hi, -1))
- assert not list(daterange(hi, lo))
- assert not list(daterange(lo, hi, -7))
- assert not list(daterange(hi, lo, 7))
- for d in map( lambda x: startDate+datetime.timedelta(days=x), xrange( (stopDate-startDate).days ) ):
- # Do stuff here
- for d in map( lambda x: startTime+x*stepTime, xrange( (stopTime-startTime).total_seconds() / stepTime.total_seconds() ) ):
- # Do stuff here
- def total_seconds( td ):
- return float(td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
Add Comment
Please, Sign In to add comment