Print All Day-Dates Between Two Dates

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.

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)

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;

}

Creating a range of dates in Python

Marginally better...

base = datetime.datetime.today()
date_list = [base - datetime.timedelta(days=x) for x in range(numdays)]

get all dates between two dates in yyyy-mm-dd

You can use datetime.strftime(your_format).

from datetime import datetime, timedelta
def date_range(future_date):
date_list = []
current = datetime.today()
end_dt = future_date
for n in range(int((end_dt - current).days)+1):
d = current + timedelta(n)
date_list.append(d.strftime('%Y-%m-%d'))
return date_list
end_dt = datetime.strptime('2022-08-10', '%Y-%m-%d')
print(date_range(end_dt))


['2022-07-20', '2022-07-21', '2022-07-22', '2022-07-23', '2022-07-24', '2022-07-25', '2022-07-26', '2022-07-27', '2022-07-28', '2022-07-29', '2022-07-30', '2022-07-31', '2022-08-01', '2022-08-02', '2022-08-03', '2022-08-04', '2022-08-05', '2022-08-06', '2022-08-07', '2022-08-08', '2022-08-09']

Printing working dates between two dates but excluding every weekends

Firstly, you can check if a value is in an iterable (such as a tuple or list) using the in keyword. isoweekday is a function, so you need to call it with isoweekday(). Finally, you need to work out the new date, then check it's weekday, otherwise it just checks if the start date is a weekend for every date.

def print_working_dates(date1, date2):
excluded = (6,7)
for n in range(int((date2 - date1).days)+1):
new = date1 + timedelta(n)
if new.isoweekday() not in excluded:
yield new

I've negated the if statement with not to make it a bit more compact.

Python Find a day dates between a range of two dates

from datetime import date, timedelta, datetime
import time

firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'

firstDay = datetime.strptime(firstDay, '%d-%B-%Y')
lastDay = datetime.strptime(lastDay, '%d-%B-%Y')
dates = [firstDay + timedelta(days=x) for x in range((lastDay-firstDay).days + 1) if (firstDay + timedelta(days=x)).weekday() == time.strptime(weekDay, '%A').tm_wday]

output

dates
[datetime.datetime(2000, 1, 3, 0, 0),
datetime.datetime(2000, 1, 10, 0, 0),
datetime.datetime(2000, 1, 17, 0, 0),
datetime.datetime(2000, 1, 24, 0, 0),
datetime.datetime(2000, 1, 31, 0, 0),
datetime.datetime(2000, 2, 7, 0, 0),
datetime.datetime(2000, 2, 14, 0, 0),
datetime.datetime(2000, 2, 21, 0, 0)]

The output in format weekDay-Month-year

[d.strftime("%A-%B-%Y") for d in dates] 

['Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-February-2000',
'Monday-February-2000',
'Monday-February-2000']

output in firstDay format: 1-January-2000

[d.strftime("%-d-%B-%Y") for d in dates]   
['3-January-2000',
'10-January-2000',
'17-January-2000',
'24-January-2000',
'31-January-2000',
'7-February-2000',
'14-February-2000',
'21-February-2000']

Iterating through a range of dates in Python

Why are there two nested iterations? For me it produces the same list of data with only one iteration:

for single_date in (start_date + timedelta(n) for n in range(day_count)):
print ...

And no list gets stored, only one generator is iterated over. Also the "if" in the generator seems to be unnecessary.

After all, a linear sequence should only require one iterator, not two.

Update after discussion with John Machin:

Maybe the most elegant solution is using a generator function to completely hide/abstract the iteration over the range of dates:

from datetime import date, timedelta

def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + timedelta(n)

start_date = date(2013, 1, 1)
end_date = date(2015, 6, 2)
for single_date in daterange(start_date, end_date):
print(single_date.strftime("%Y-%m-%d"))

NB: For consistency with the built-in range() function this iteration stops before reaching the end_date. So for inclusive iteration use the next day, as you would with range().



Related Topics



Leave a reply



Submit