Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:52:49+00:00 2026-06-17T10:52:49+00:00

Possible Duplicate: Iterating through a range of dates in Python I have two entries:

  • 0

Possible Duplicate:
Iterating through a range of dates in Python

I have two entries:

date1 = 2004.09.25
date2 = 2004.10.08

I want to wrote a script in Python that recognizes the date range and print them. something like this:

for i in range(date1:date2):
    print i

Do I need to define the dates in a particular format for dates?
The expected output is like:

2004.09.25
2004.09.26
.
.
.
2004.10.08
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:52:50+00:00Added an answer on June 17, 2026 at 10:52 am

    Your first step should have been to look at the python datetime library.

    Overall, your first solution could look something like this:

    date1 = datetime.date(2004, 9, 25)
    date2 = datetime.date(2004, 10, 8)
    day = datetime.timedelta(days=1)
    
    while date1 <= date2:
        print date1.strftime('%Y.%m.%d')
        date1 = date1 + day
    

    (one thing to note: this will obviously clobber your date1 variable)

    I would later refactor this into a daterange function so that you can do something closer to what you did; it would look like

    for d in daterange(date1, date2):
        print d.strftime('%Y.%m.%d')
    

    Later on, when you develop your python skills, it could like like this:

    for i in range((date2 - date1).days + 1):
        print (date1 + datetime.timedelta(days=i)).strftime('%Y.%m.%d')
    

    Or this, which would be my final version:

    def daterange(d1, d2):
        return (d1 + datetime.timedelta(days=i) for i in range((d2 - d1).days + 1))
    
    for d in daterange(date1, date2):
        print d.strftime('%Y.%m.%d')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C# Iterating through an enum? (Indexing a System.Array) All I want to
Possible Duplicate: Simultaneously Iterating Over Two Sets of Elements in jQuery I have two
Possible Duplicate: Remove items from a list while iterating in Python I want to
Possible Duplicate: Iterating through stdClass obj’s that are contained within a parent stdClass Obj.
Possible Duplicate: Iterating through a String and replacing single chars with substrings in haskell
Possible Duplicate: for vs foreach vs while which is faster for iterating through arrays
Possible Duplicate: Modifying list while iterating I have been given a task to make
Possible Duplicate: How to get line count cheaply in Python? Good day. i have
Possible Duplicate: Python: Adding element to list while iterating This doesn't seem to work,
Possible Duplicate: Looping through Markers with Google Maps API v3 Problem I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.