Posts: 61
Threads: 29
Joined: Oct 2023
Jan-31-2024, 03:40 PM
(This post was last modified: Jan-31-2024, 03:40 PM by jacksfrustration.)
trying to make a list which i will consist of the dates in a (date day month year) format of the past three months. i finally came up with the upcoming list comprehension
dates = [dt.strftime('%a %d %B %Y') for dt in cal.Calendar().itermonthdates(year, month) if (dt.month==month-1) or (dt.month == month) or dt.month==month+1] the problem is when i print the list it only contains january and the first 5 days of february. my end goal is to make this list and then make a shorter list to use in an optionmenu that consists of the past 5 and upcoming 5 days. for the last list i have the following code
for index, dat in enumerate(self.dates_list):
if dat == datetime.today().strftime('%a %d %B %Y'):
i=self.updated_list.index(dat)
self.updated_list.extend(self.dates_list[i - 5:i+6]) can anyone help me make the list of the 3 months mentioned on the title of this post? In this case it would be December, January and February
Posts: 453
Threads: 16
Joined: Jun 2022
Jan-31-2024, 06:05 PM
(This post was last modified: Jan-31-2024, 06:05 PM by rob101.)
There will be a number of different ways to do this, but, I'd be looking at using a timedelta object (maybe more than one) to look backwards and forwards, 45 days, either side of the current date (91 days, being 3 months).
I'd also split that very long list comprehension line, into shorter, more 'human friendly' code, so that it's easier to understand and see what's going on.
But, that's just how I code, which is fine for me and may not be fine for you, or anyone else, for that matter.
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 6,794
Threads: 20
Joined: Feb 2020
Jan-31-2024, 07:15 PM
(This post was last modified: Jan-31-2024, 07:15 PM by deanhystad.)
Use the tkcalendar module.
import tkinter as tk
from tkcalendar import DateEntry
root = tk.Tk()
date_entry = DateEntry(root)
date_entry.pack(padx=10, pady=10)
root.mainloop() This is not a standard library. You'll need to install the package, but the calendar layout is sooooo much better than a list of dates.
To answer your question:
I would make a generator using ideas mentioned in rob101's post.
from datetime import datetime, timedelta
def dateiter(days=1, format="%a %d %B %Y", start=0):
"""Return days number of datestrings. Default starting
date is today. You can specify a relative date using an
integer for start (-1 is yesterday), or you can pass a
datetime object.
"""
if isinstance(start, int):
start = datetime.now() + timedelta(days=start)
for x in range(days):
yield (start + timedelta(days=x)).strftime(format)
print(*dateiter(5), sep=", ")
print(*dateiter(5, start=-2), sep=", ")
print(*dateiter(5, start=-2, format="%b %d"), sep=", ") Output: Wed 31 January 2024, Thu 01 February 2024, Fri 02 February 2024, Sat 03 February 2024, Sun 04 February 2024
Mon 29 January 2024, Tue 30 January 2024, Wed 31 January 2024, Thu 01 February 2024, Fri 02 February 2024
Jan 29, Jan 30, Jan 31, Feb 01, Feb 02
Posts: 61
Threads: 29
Joined: Oct 2023
Feb-03-2024, 05:05 PM
(This post was last modified: Feb-03-2024, 05:44 PM by jacksfrustration.)
(Jan-31-2024, 07:15 PM)deanhystad Wrote: Use the tkcalendar module.
import tkinter as tk
from tkcalendar import DateEntry
root = tk.Tk()
date_entry = DateEntry(root)
date_entry.pack(padx=10, pady=10)
root.mainloop() This is not a standard library. You'll need to install the package, but the calendar layout is sooooo much better than a list of dates.
To answer your question:
I would make a generator using ideas mentioned in rob101's post.
from datetime import datetime, timedelta
def dateiter(days=1, format="%a %d %B %Y", start=0):
"""Return days number of datestrings. Default starting
date is today. You can specify a relative date using an
integer for start (-1 is yesterday), or you can pass a
datetime object.
"""
if isinstance(start, int):
start = datetime.now() + timedelta(days=start)
for x in range(days):
yield (start + timedelta(days=x)).strftime(format)
print(*dateiter(5), sep=", ")
print(*dateiter(5, start=-2), sep=", ")
print(*dateiter(5, start=-2, format="%b %d"), sep=", ") Output: Wed 31 January 2024, Thu 01 February 2024, Fri 02 February 2024, Sat 03 February 2024, Sun 04 February 2024
Mon 29 January 2024, Tue 30 January 2024, Wed 31 January 2024, Thu 01 February 2024, Fri 02 February 2024
Jan 29, Jan 30, Jan 31, Feb 01, Feb 02
that is nice but i need to make a list of the 5 previous days as well as the 5 upcoming. I tried with timedelta but it doesnt seem to work. I also tried tkcalendar but it seems to create an interface whereas i need to make a list to use with the optionmenu
quick edit: i managed to make it work. thanks guys for your help
Posts: 6,794
Threads: 20
Joined: Feb 2020
Feb-03-2024, 06:37 PM
(This post was last modified: Feb-03-2024, 06:37 PM by deanhystad.)
dayiter(11, start=-5) would return 11 days starting 5 days ago.
|