Python Forum
Make list of dates between today back to n days
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Make list of dates between today back to n days
#1
Hi,
I want to make list of dates from now back to n days (for example 2 days) with time interval 12hrs.

I use the bewlow code, but its giving me empty.

from datetime import date, datetime, timedelta
def datetime_range(start, end, delta):
    current = start
    print(current)
    if not isinstance(delta, timedelta):
        delta = timedelta(delta)
    while current < end:
        yield current
        current += delta


interval=12 #12hrs
n_days=2
start = date.today()
end=start_dt-timedelta(days=n_days)
dates_list =[]
for dt in datetime_range(start, end,interval):
    dt_m = (dt-timedelata(hours=12)).strftime("%Y-%m-%d %H:%M:%S")
    dates_list.append(dt_m)
my desired output is: for example time now is 2020-10-01 18:10:10

my_list of dates:

[2020-09-29 18:10:10 2020-09-30 06:10:10 2020-09-30 18:10:10 2020-10-01 06:10:10 2020-10-01 18:10:10]
Reply
#2
Are you sure it giving 'empty' and not 'NameError'?

There is no 'start_dt' and there is 'timedelata' as well. There is no way you can get empty list from this snippet.

EDIT: and there is difference between today and now :-)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
I corrected, but still give empty (no error)

from datetime import date, datetime, timedelta
def datetime_range(start, end, delta):
    current = start
    print(current)
    if not isinstance(delta, timedelta):
        delta = timedelta(delta)
    while current < end:
        yield current
        current += delta


interval=12 #12hrs
n_days=3
start = date.today()
end=start-timedelta(days=n_days)
dates_list =[]
for dt in datetime_range(start, end,interval):
    dt_m = (dt-timedelata(hours=12)).strftime("%Y-%m-%d %H:%M:%S")
    dates_list.append(dt_m)

print(dates_list)
Reply
#4
You want to use an interval of 12 hours. So you must calculate with datetime, not date. You can leave date out from the import.

In line 6 you must make clear you delta is the number of hours:
delta = timedelta(hours=delta)
The while on line 7 is upside down:
    while current < end:
        yield current
        current += delta
But you are counting down. So it must be:
    while current > end:
        yield current
        current -= delta
As we are calculating in datetime you must change line 14, instead of date.today() it must be:

start = datetime.now()
In line 18 you must correct a typo: "timedelata" must be "timedelta".
Output:
2020-10-03 14:22:09.268970 ['2020-10-03 02:22:09', '2020-10-02 14:22:09', '2020-10-02 02:22:09', '2020-10-01 14:22:09', '2020-10-01 02:22:09', '2020-09-30 14:22:09']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  get list of dates in past month, current and upcoming month jacksfrustration 4 474 Feb-03-2024, 06:37 PM
Last Post: deanhystad
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 505 Oct-03-2023, 11:15 AM
Last Post: snippsat
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,256 May-22-2023, 10:39 PM
Last Post: ICanIBB
  help me to make my password list in python >>> Oktay34riza 0 552 Dec-23-2022, 12:38 PM
Last Post: Oktay34riza
  How split N days between specified start & end days SriRajesh 2 1,302 May-06-2022, 02:12 PM
Last Post: SriRajesh
  Make Groups with the List Elements quest 2 1,934 Jul-11-2021, 09:58 AM
Last Post: perfringo
  Need to identify only files created today. tester_V 5 4,552 Feb-18-2021, 06:32 AM
Last Post: tester_V
Question How to make a 3D List of Excel Spreadsheets? chatguy 4 2,678 Jan-24-2021, 05:24 AM
Last Post: buran
  Undo interation to make a single list? DustinKlent 2 2,133 Nov-29-2020, 03:41 AM
Last Post: DustinKlent

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020