Loop through a generator that returns a date iterator in Python

Sample code

from datetime import date, datetime, timedelta

#Generator function that returns an iterator for a date object
#start date: begin
#End date: end
def date_iterator_generator(begin, end):
  #Find the number of days from the timedelta of the end date and start date
  #To include the end date+1
  length = (end - begin).days + 1
  #Numerical sequence from 0 to days
  for n in range(length):
    yield begin + timedelta(n)

#Start date / end date
begin = datetime.strptime('20200331', '%Y%m%d').date()
end   = date.today() #Today's date

#Generate generator
gen = date_iterator_generator(begin, end)
print('gen: ' + str(gen))

#Output date
for target in gen:
  print(target.strftime('%Y%m%d'))

Execution result

The result of running with Python 3.8.2.

gen: <generator object date_iterator_generator at 0x104e0f900>
20200331
20200401
20200402
20200403
20200404
20200405

Reference material

Functional Programming HOWTO — Python 3 \ .8 \ .2 Documentation

Generators are special functions that make it easier to write iterators. A standard function calculates and returns a value, but the generator returns an iterator that returns a set of values.

Built-in — Python 3 \ .8 \ .2 documentation

The range type represents an immutable sequence of numbers and is commonly used in for loops for a certain number of loops.

datetime ---Basic date and time types — Python 3 \ .8 \ .2 documentation

The timedelta object represents the elapsed time, that is, the difference between two dates or times.

Recommended Posts

Loop through a generator that returns a date iterator in Python
A memo that I wrote a quicksort in Python
A program that removes duplicate statements in Python
Rewriting elements in a loop of lists (Python)
Create a loop antenna pattern in Python in KiCad
Date manipulation in Python
Get date in Python
Date calculation in python
Zundokokiyoshi (generator) in Python
Date calculation in Python
What's in that variable (when running a Python script)
In Python, create a decorator that dynamically accepts arguments Create a decorator
Modules that may go through the shell in Python
MALSS, a tool that supports machine learning in Python
Take a screenshot in Python
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
Create a function in Python
Published a library that hides character data in Python images
[Python] Create a linebot that draws any date on a photo
Let's create a script that registers with Ideone.com in Python.
I tried "a program that removes duplicate statements in Python"
Make a bookmarklet in Python
Create code that outputs "A and pretending B" in python
[MQTT / Python] Implemented a class that does MQTT Pub / Sub in Python
String date manipulation in Python
Draw a heart in Python
A set of script files that do wordcloud in Python3
Sort by date in python
I made a garbled generator that encodes favorite sentences from UTF-8 to Shift-JIS (cp932) in Python
Play a sound in Python assuming that the keyboard is a piano keyboard
A memo that handles double-byte double quotes in Python regular expressions
IQ Bot Custom Logic (Python): Efficient replacement process in a loop
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
Easy! Implement a Twitter bot that runs on Heroku in Python
[Python / Django] Create a web API that responds in JSON format
A record that GAMEBOY could not be done in Python. (PYBOY)
About psd-tools, a library that can process psd files in Python
A function that measures the processing time of a method in python
IQ Bot Custom Logic (Python): Efficient replacement process in a loop
A program that determines whether a number entered in Python is a prime number
A script that returns 0, 1 attached to the first Python prime number
A class that summarizes frequently used methods in twitter api (python)
What's new in datetime that is a bit more useful in Python 3
Released erajp that displays date time in Japanese calendar format in Python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
Create a DI Container in Python
Draw a scatterplot matrix in python
Implement a date setter in Tkinter
A function that returns a random name
Infinite prime number generator in Python3
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Create a binary file in Python
Split iterator into chunks in python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python