Date Function to Display All Dates Between Two Dates

Date function to display all dates between two dates

There is the DatePeriod class.

EXAMPLE:

$begin = new DateTime('2013-02-01');
$end = new DateTime('2013-02-13');

$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);

foreach($daterange as $date){
echo $date->format("Y-m-d") . "<br>";
}

(P1D stands for period of one day, see DateInterval for further documentation)

Python generating a list of dates between two dates

You can use pandas.date_range() for this:

import pandas
pandas.date_range(sdate,edate-timedelta(days=1),freq='d')


DatetimeIndex(['2019-03-22', '2019-03-23', '2019-03-24', '2019-03-25',
'2019-03-26', '2019-03-27', '2019-03-28', '2019-03-29',
'2019-03-30', '2019-03-31', '2019-04-01', '2019-04-02',
'2019-04-03', '2019-04-04', '2019-04-05', '2019-04-06',
'2019-04-07', '2019-04-08'],
dtype='datetime64[ns]', freq='D')

Javascript - get array of dates between 2 dates

function (startDate, endDate, addFn, interval) {

addFn = addFn || Date.prototype.addDays;
interval = interval || 1;

var retVal = [];
var current = new Date(startDate);

while (current <= endDate) {
retVal.push(new Date(current));
current = addFn.call(current, interval);
}

return retVal;

}

Get a list of dates between two dates using a function

Try something like this:

CREATE FUNCTION dbo.ExplodeDates(@startdate datetime, @enddate datetime)
returns table as
return (
with
N0 as (SELECT 1 as n UNION ALL SELECT 1)
,N1 as (SELECT 1 as n FROM N0 t1, N0 t2)
,N2 as (SELECT 1 as n FROM N1 t1, N1 t2)
,N3 as (SELECT 1 as n FROM N2 t1, N2 t2)
,N4 as (SELECT 1 as n FROM N3 t1, N3 t2)
,N5 as (SELECT 1 as n FROM N4 t1, N4 t2)
,N6 as (SELECT 1 as n FROM N5 t1, N5 t2)
,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N6)
SELECT DATEADD(day,num-1,@startdate) as thedate
FROM nums
WHERE num <= DATEDIFF(day,@startdate,@enddate) + 1
);

You then use:

SELECT *
FROM dbo.ExplodeDates('20090401','20090531') as d;

Edited (after the acceptance):

Please note... if you already have a sufficiently large nums table then you should use:

CREATE FUNCTION dbo.ExplodeDates(@startdate datetime, @enddate datetime)
returns table as
return (
SELECT DATEADD(day,num-1,@startdate) as thedate
FROM nums
WHERE num <= DATEDIFF(day,@startdate,@enddate) + 1
);

And you can create such a table using:

CREATE TABLE dbo.nums (num int PRIMARY KEY);
INSERT dbo.nums values (1);
GO
INSERT dbo.nums SELECT num + (SELECT COUNT(*) FROM nums) FROM nums
GO 20

These lines will create a table of numbers containing 1M rows... and far quicker than inserting them one by one.

You should NOT create your ExplodeDates function using a function that involves BEGIN and END, as the Query Optimizer becomes unable to simplify the query at all.

How to get all the dates between two dates?

An alternative solution to your problem is using pandas

import pandas as pd
pd.date_range(start=start_date,end=end_date)

Get all Dates between 2 Dates Javascript

you are on the right way

setHours could have an issue with timezone try to use setUTCHours;

and reset start and end dates before any calculations

try out:

function getDatesInRange(startDate, endDate) {
const start = new Date(new Date(startDate).setUTCHours(0, 0, 0, 0));
const end = new Date(new Date(endDate).setUTCHours(0, 0, 0, 0));

const date = new Date(start.getTime());

const dates = [];

while (date <= end) {
dates.push(new Date(date));
date.setDate(date.getDate() + 1);
}

return dates;
}

const travel_start = "2022-03-20T05:59:57.118Z";
const travel_start2 ="2022-03-20T23:59:57.118Z";

const travel_end = "2022-03-23T20:00:57.118Z";

const res1 = getDatesInRange(travel_start, travel_end);
const res2 = getDatesInRange(travel_start2,travel_end);

console.log('res1', res1)
console.log('res2', res2)

How to get all dates between two dates given days of the week?

Based on answer of @JohnHartsock we can create the following function which

  • populates range of dates between firstDay and lastDay
  • filter dates based on of daysOfWeek

var days = {Sun: 0, Mon: 1, Tue: 2, Wed: 3, Thu: 4, Fri:5, Sat:6};
function findClassDays(daysOfWeek, firstDay, lastDay) { let classDays = []; let rangeDates = getDates(new Date(firstDay), new Date(lastDay)); classDays = rangeDates.filter(f => daysOfWeek.some((d, i) => days[d]== f.getDay())); return classDays;}
function getDates(startDate, stopDate) { var dateArray = new Array(); var currentDate = new Date(startDate); while (currentDate <= stopDate) { dateArray.push(new Date (currentDate)); currentDate = currentDate.addDays(1); } return dateArray;}
Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date;}
console.log(findClassDays(['Mon', 'Wed', 'Fri'], '2019-12-01', '2019-12-15'));

Get all dates between two dates in SQL Server

My first suggestion would be use your calendar table, if you don't have one, then create one. They are very useful. Your query is then as simple as:

DECLARE @MinDate DATE = '20140101',
@MaxDate DATE = '20140106';

SELECT Date
FROM dbo.Calendar
WHERE Date >= @MinDate
AND Date < @MaxDate;

If you don't want to, or can't create a calendar table you can still do this on the fly without a recursive CTE:

DECLARE @MinDate DATE = '20140101',
@MaxDate DATE = '20140106';

SELECT TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1)
Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b;

For further reading on this see:

  • Generate a set or sequence without loops – part 1
  • Generate a set or sequence without loops – part 2
  • Generate a set or sequence without loops – part 3

With regard to then using this sequence of dates in a cursor, I would really recommend you find another way. There is usually a set based alternative that will perform much better.

So with your data:

  date   | it_cd | qty 
24-04-14 | i-1 | 10
26-04-14 | i-1 | 20

To get the quantity on 28-04-2014 (which I gather is your requirement), you don't actually need any of the above, you can simply use:

SELECT  TOP 1 date, it_cd, qty 
FROM T
WHERE it_cd = 'i-1'
AND Date <= '20140428'
ORDER BY Date DESC;

If you don't want it for a particular item:

SELECT  date, it_cd, qty 
FROM ( SELECT date,
it_cd,
qty,
RowNumber = ROW_NUMBER() OVER(PARTITION BY ic_id
ORDER BY date DESC)
FROM T
WHERE Date <= '20140428'
) T
WHERE RowNumber = 1;

Calculate all dates between 2 dates in php?

Without loop using range() & array_map() :

EDIT: a little mistake, you have to jump 86400, because 1 day = 86400 seconds, so the code should be fine now :)


    $st_date = '2012-07-20';
$ed_date = '2012-07-27';
$dates = range(strtotime($st_date), strtotime($ed_date),86400);
$range_of_dates = array_map("toDate", $dates);
print_r($range_of_dates);
function toDate($x){return date('Y-m-d', $x);}

?>


Related Topics



Leave a reply



Submit