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 7180303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:19:07+00:00 2026-05-28T17:19:07+00:00

I have two date ranges where each range is determined by a start and

  • 0

I have two date ranges where each range is determined by a start and end date (obviously, datetime.date instances). The two ranges can overlap or not. I need the number of days of the overlap. Of course I can pre-fill two sets with all dates within both ranges and the perform a set intersection but this is possibly inefficient…is there a better way apart from another solution using a long if–elif section covering all cases?

  • 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-05-28T17:19:08+00:00Added an answer on May 28, 2026 at 5:19 pm
    • Determine the latest of the two start dates and the earliest of the two end dates.
    • Compute the timedelta by subtracting them.
    • If the delta is positive, that is the number of days of overlap.

    Here is an example calculation:

    >>> from datetime import datetime
    >>> from collections import namedtuple
    >>> Range = namedtuple('Range', ['start', 'end'])
    
    >>> r1 = Range(start=datetime(2012, 1, 15), end=datetime(2012, 5, 10))
    >>> r2 = Range(start=datetime(2012, 3, 20), end=datetime(2012, 9, 15))
    >>> latest_start = max(r1.start, r2.start)
    >>> earliest_end = min(r1.end, r2.end)
    >>> delta = (earliest_end - latest_start).days + 1
    >>> overlap = max(0, delta)
    >>> overlap
    52
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to implement a solution where two date ranges can overlap each other.
I'm using the jQuery datepicker where I have two textboxes (start date / end
I have two datetime pickers that I'm using to set a start and end
I have two Java instances of java.util.Date and I have to find out if
I have two Ajax Toolkit calendar extenders. One of them is a start date
I have two DateTime variables first period and second period and with each period
I have two date entities, start_date and end_date and now I want to find
I have two Zend Date objects to work with: $now = Zend_Date::now(); $sessionStart =
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.
I have two dates: how do I compare which one is greater date in

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.