Python Forum

Full Version: How split N days between specified start & end days
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I have start date and end date. I want to split it to N-equal days.
start_date = "2022/04/10 00:00:00"
end_date = "2022/04/14 00:00:00"
I want to make list of N-equal days ["2022/04/10 00:00:00", "2022/04/11 00:00:00", "2022/04/12 00:00:00","2022/04/13 00:00:00", "2022/04/14 00:00:00"]. Some one can help how to achieve this. Interval of days may be 1, or 2, x.
When you have task like this you most give it try to show some effort🔨
Can do it with datatime (a little more work) or Pandas pandas.date_range or my favorite Pendulum range().
>>> import pendulum
>>> 
>>> start_date = "2022/04/10 00:00:00"
>>> start_date = "2022/04/10 00:00:00"
>>> # Parse it to Datetime object
>>> start_date = pendulum.parse(start_date)
>>> end_date = pendulum.parse(end_date)
>>> period = pendulum.period(start_date, end_date)
>>> for dt in period.range('days'):
...     print(dt)   
...     
2022-04-10T00:00:00+00:00
2022-04-11T00:00:00+00:00
2022-04-12T00:00:00+00:00
2022-04-13T00:00:00+00:00
2022-04-14T00:00:00+00:00 
thanks