Get a List of Dates Between Two Dates

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

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)

Print all day-dates between two dates

I came up with this:

from datetime import date, timedelta

start_date = date(2008, 8, 15)
end_date = date(2008, 9, 15) # perhaps date.now()

delta = end_date - start_date # returns timedelta

for i in range(delta.days + 1):
day = start_date + timedelta(days=i)
print(day)

The output:

2008-08-15
2008-08-16
...
2008-09-13
2008-09-14
2008-09-15

Your question asks for dates in-between but I believe you meant including the start and end points, so they are included. To remove the end date, delete the "+ 1" at the end of the range function. To remove the start date, insert a 1 argument to the beginning of the range function.

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

this few lines are the simple answer for this question in sql server.

WITH mycte AS
(
SELECT CAST('2011-01-01' AS DATETIME) DateValue
UNION ALL
SELECT DateValue + 1
FROM mycte
WHERE DateValue + 1 < '2021-12-31'
)

SELECT DateValue
FROM mycte
OPTION (MAXRECURSION 0)

Generate list of months between interval in python

>>> from datetime import datetime, timedelta
>>> from collections import OrderedDict
>>> dates = ["2014-10-10", "2016-01-07"]
>>> start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
>>> OrderedDict(((start + timedelta(_)).strftime(r"%b-%y"), None) for _ in xrange((end - start).days)).keys()
['Oct-14', 'Nov-14', 'Dec-14', 'Jan-15', 'Feb-15', 'Mar-15', 'Apr-15', 'May-15', 'Jun-15', 'Jul-15', 'Aug-15', 'Sep-15', 'Oct-15', 'Nov-15', 'Dec-15', 'Jan-16']

Update: a bit of explanation, as requested in one comment. There are three problems here: parsing the dates into appropriate data structures (strptime); getting the date range given the two extremes and the step (one month); formatting the output dates (strftime). The datetime type overloads the subtraction operator, so that end - start makes sense. The result is a timedelta object that represents the difference between the two dates, and the .days attribute gets this difference expressed in days. There is no .months attribute, so we iterate one day at a time and convert the dates to the desired output format. This yields a lot of duplicates, which the OrderedDict removes while keeping the items in the right order.

Now this is simple and concise because it lets the datetime module do all the work, but it's also horribly inefficient. We're calling a lot of methods for each day while we only need to output months. If performance is not an issue, the above code will be just fine. Otherwise, we'll have to work a bit more. Let's compare the above implementation with a more efficient one:

from datetime import datetime, timedelta
from collections import OrderedDict

dates = ["2014-10-10", "2016-01-07"]

def monthlist_short(dates):
start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
return OrderedDict(((start + timedelta(_)).strftime(r"%b-%y"), None) for _ in xrange((end - start).days)).keys()

def monthlist_fast(dates):
start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
total_months = lambda dt: dt.month + 12 * dt.year
mlist = []
for tot_m in xrange(total_months(start)-1, total_months(end)):
y, m = divmod(tot_m, 12)
mlist.append(datetime(y, m+1, 1).strftime("%b-%y"))
return mlist

assert monthlist_fast(dates) == monthlist_short(dates)

if __name__ == "__main__":
from timeit import Timer
for func in "monthlist_short", "monthlist_fast":
print func, Timer("%s(dates)" % func, "from __main__ import dates, %s" % func).timeit(1000)

On my laptop, I get the following output:

monthlist_short 2.3209939003
monthlist_fast 0.0774540901184

The concise implementation is about 30 times slower, so I would not recommend it in time-critical applications :)

List of dates between two given dates in pandas dataframe

You can retrieve date range using the mentioned pd.date_range as follows:

import pandas as pd
from datetime import date, timedelta

start_date, end_date = date(2020, 9, 15), date(2020, 11 , 1)

date_range = pd.date_range(start_date, end_date - timedelta(days = 1), freq='d')

Which gives the output:

DatetimeIndex(['2020-09-15', '2020-09-16', '2020-09-17', '2020-09-18',
'2020-09-19', '2020-09-20', '2020-09-21', '2020-09-22',
'2020-09-23', '2020-09-24', '2020-09-25', '2020-09-26',
'2020-09-27', '2020-09-28', '2020-09-29', '2020-09-30',
'2020-10-01', '2020-10-02', '2020-10-03', '2020-10-04',
'2020-10-05', '2020-10-06', '2020-10-07', '2020-10-08',
'2020-10-09', '2020-10-10', '2020-10-11', '2020-10-12',
'2020-10-13', '2020-10-14', '2020-10-15', '2020-10-16',
'2020-10-17', '2020-10-18', '2020-10-19', '2020-10-20',
'2020-10-21', '2020-10-22', '2020-10-23', '2020-10-24',
'2020-10-25', '2020-10-26', '2020-10-27', '2020-10-28',
'2020-10-29', '2020-10-30', '2020-10-31'],
dtype='datetime64[ns]', freq='D')

You can check if a date is in a given range using isin():

date_range.isin(['2020-10-29']).any()

It can be converted to string:

date_range1 = [str(d.date()) for d in date_range]

Which gives the output:

['2020-09-15', '2020-09-16', '2020-09-17', '2020-09-18', '2020-09-19', '2020-09-20', '2020-09-21', '2020-09-22', '2020-09-23', '2020-09-24', '2020-09-25', '2020-09-26', '2020-09-27', '2020-09-28', '2020-09-29', '2020-09-30', '2020-10-01', '2020-10-02', '2020-10-03', '2020-10-04', '2020-10-05', '2020-10-06', '2020-10-07', '2020-10-08', '2020-10-09', '2020-10-10', '2020-10-11', '2020-10-12', '2020-10-13', '2020-10-14', '2020-10-15', '2020-10-16', '2020-10-17', '2020-10-18', '2020-10-19', '2020-10-20', '2020-10-21', '2020-10-22', '2020-10-23', '2020-10-24', '2020-10-25', '2020-10-26', '2020-10-27', '2020-10-28', '2020-10-29', '2020-10-30', '2020-10-31']

Return all dates between two dates as a list in flutter date range picker

Try the following :

List<DateTime> getDaysInBetween(DateTime startDate, DateTime endDate) {
List<DateTime> days = [];
for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
days.add(startDate.add(Duration(days: i)));
}
return days;
}


Related Topics



Leave a reply



Submit