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

  • SEARCH
  • Home
  • 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 7969011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:10:08+00:00 2026-06-04T07:10:08+00:00

Given two datetimes ( start_date and end_date ), I’d like to generate a list

  • 0

Given two datetimes (start_date and end_date), I’d like to generate a list of other datetimes between these two dates, the new datetimes being separated by a variable interval. e.g. every 4 days between 2011-10-10 and 2011-12-12 or every 8 hours between now and tomorrow 19p.m.

Maybe something roughly equivalent to the Dateperiod PHP class.

What would be the most efficient way to accomplish this in Python?

  • 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-04T07:10:09+00:00Added an answer on June 4, 2026 at 7:10 am

    Use datetime.timedelta:

    from datetime import date, datetime, timedelta
    
    def perdelta(start, end, delta):
        curr = start
        while curr < end:
            yield curr
            curr += delta
    
    >>> for result in perdelta(date(2011, 10, 10), date(2011, 12, 12), timedelta(days=4)):
    ...     print result
    ...
    2011-10-10
    2011-10-14
    2011-10-18
    2011-10-22
    2011-10-26
    2011-10-30
    2011-11-03
    2011-11-07
    2011-11-11
    2011-11-15
    2011-11-19
    2011-11-23
    2011-11-27
    2011-12-01
    2011-12-05
    2011-12-09
    

    Works for both dates and datetime objects. Your second example:

    >>> for result in perdelta(datetime.now(),
    ...         datetime.now().replace(hour=19) + timedelta(days=1),
    ...         timedelta(hours=8)):
    ...     print result
    ... 
    2012-05-21 17:25:47.668022
    2012-05-22 01:25:47.668022
    2012-05-22 09:25:47.668022
    2012-05-22 17:25:47.668022
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get the number of weekdays between two given dates without just
Given two date/times: @start_date = '2009-04-15 10:24:00.000' @end_date = '2009-04-16 19:43:01.000' Is it possible
I would like to detect if a given day is between two days of
I've have a problem trying to print a given period between two dates. Let
Given two dates, what is the best method to calculate the number of days
Given two planes positioned and rotated arbitrary to each other, I'm trying to determine
Example: given two dates below, finish is always greater than or equal to start
I'm trying to find missing dates between two DateTime variables for a collection of
I want to compare between two dates. From both the dates, I am fetching
Given two DateTimes in C#, how can I display the difference in years and

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.