Calculate the number of records for each date between 2 dates

Improving on Dale K’s answer, I suggest you use a tally table or function, as this is usually more performant. I have used Itzik Ben-Gan’s well-known one below: DECLARE @StartDate date=”2020-11-01″, @EndDate date=”2021-02-22″; WITH L0 AS ( SELECT 1 AS c FROM (VALUES(1),(1),(1),(1),(1),(1),(1),(1), (1),(1),(1),(1),(1),(1),(1),(1)) AS D(c) ), L1 AS ( SELECT 1 AS c FROM … Read more

Efficient date range overlap calculation?

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’, … Read more

Merge pandas dataframes where one value is between two others [duplicate]

As you say, this is pretty easy in SQL, so why not do it in SQL? import pandas as pd import sqlite3 #We’ll use firelynx’s tables: presidents = pd.DataFrame({“name”: [“Bush”, “Obama”, “Trump”], “president_id”:[43, 44, 45]}) terms = pd.DataFrame({‘start_date’: pd.date_range(‘2001-01-20′, periods=5, freq=’48M’), ‘end_date’: pd.date_range(‘2005-01-21′, periods=5, freq=’48M’), ‘president_id’: [43, 43, 44, 44, 45]}) war_declarations = pd.DataFrame({“date”: [datetime(2001, … Read more

How to check if a date is in a given range?

Converting them to timestamps is the way to go alright, using strtotime, e.g. $start_date=”2009-06-17″; $end_date=”2009-09-05″; $date_from_user=”2009-08-28″; check_in_range($start_date, $end_date, $date_from_user); function check_in_range($start_date, $end_date, $date_from_user) { // Convert to timestamp $start_ts = strtotime($start_date); $end_ts = strtotime($end_date); $user_ts = strtotime($date_from_user); // Check that user date is between start & end return (($user_ts >= $start_ts) && ($user_ts <= $end_ts)); … Read more

Javascript – get array of dates between 2 dates

Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } function getDates(startDate, stopDate) { var dateArray = new Array(); var currentDate = startDate; while (currentDate <= stopDate) { dateArray.push(new Date (currentDate)); currentDate = currentDate.addDays(1); } return dateArray; } Here is a functional demo http://jsfiddle.net/jfhartsock/cM3ZU/