3 ms·
I think it's just the relative simplicity of code that handles date logic compared to other languages: https://stackoverflow.com/questions/4345045/javascript-l
by mywittyname 7y ago
I think it's just the relative simplicity of code that handles date logic compared to other languages:
https://stackoverflow.com/questions/4345045/javascript-loop-between-date-ranges https://stackoverflow.com/questions/4345045/javascript-loop-...
I hate dealing with date math, but I have to admit that Python makes date math much more elegant. I've written the functional equivalent of the OP function several times, and it's just so nice to be able to loop over dates just like they were any other generic sequence.
Edit: plus you can enhance it pretty to make arbitrary increments with ease:
def daterange(start, stop, increment='days', step=1):
start = datetime.fromisoformat(start)
stop = datetime.fromisoformat(stop)
inc = {increment: step}
while start <= stop:
yield start
start += timedelta(**inc)
> list(daterange('2020-01-01', '2020-01-14'))
[datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 2, 0, 0), datetime.datetime(2020, 1, 3, 0, 0), [...], datetime.datetime(2020, 1, 14, 0, 0)]
> list(daterange('2020-01-01', '2020-01-14', 'weeks', 1))
[datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 8, 0, 0)]
- maest 7y agoPython's date handling is meh. Some languages have dates as first class primitives and are a dream to work with.