Create an Array or List of All Dates Between Two Dates

Create an array or List of all dates between two dates

LINQ:

Enumerable.Range(0, 1 + end.Subtract(start).Days)
.Select(offset => start.AddDays(offset))
.ToArray();

For loop:

var dates = new List<DateTime>();

for (var dt = start; dt <= end; dt = dt.AddDays(1))
{
dates.Add(dt);
}

EDIT:
As for padding values with defaults in a time-series, you could enumerate all the dates in the full date-range, and pick the value for a date directly from the series if it exists, or the default otherwise. For example:

var paddedSeries = fullDates.ToDictionary(date => date, date => timeSeries.ContainsDate(date) 
? timeSeries[date] : defaultValue);

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;

}

PHP: Return all dates between two dates in an array

You could also take a look at the DatePeriod class:

$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);

Which should get you an array with DateTime objects.

To iterate

foreach ($period as $key => $value) {
//$value->format('Y-m-d')
}

Visual C#, An array of Dates between two Dates

I advise you to use lists instead arrays and u can use Enumarable.Range

var startDate = new DateTime(2013, 1, 25);
var endDate = new DateTime(2013, 1, 31);
int days = (endDate - startDate).Days + 1; // incl. endDate

List<DateTime> range = Enumerable.Range(0, days)
.Select(i => startDate.AddDays(i))
.ToList();

You can learn much more about Lists here

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')

Find the each dates between two dates C#

We can do it with a loop for clarity.

            DateTime futurDate = Convert.ToDateTime("08/21/2016");
DateTime TodayDate = DateTime.Now;

var days = (futurDate - TodayDate).Days;
var datesBetween = new List<DateTime>();

for(var i=0; i < days; i++)
{

datesBetween.Add(TodayDate.AddDays(i + 1)); //Here are your dates
}

Or with enumerables:

            var datesBetween =
Enumerable.Range(1, (futurDate - TodayDate).Days)
.Select(i => TodayDate.AddDays(i));

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)


Related Topics



Leave a reply



Submit