Python Forum
get list of dates in past month, current and upcoming month
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get list of dates in past month, current and upcoming month
#1
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
Reply
#2
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
Reply
#3
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
Reply
#4
(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
Reply
#5
dayiter(11, start=-5) would return 11 days starting 5 days ago.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why can't I copy and past only ONE specific tab? NewWorldRonin 8 828 Jan-12-2024, 06:31 PM
Last Post: deanhystad
  Python Resampling: How do I obtain the value of the last week of the month? JaneTan 2 1,023 Dec-12-2022, 12:49 AM
Last Post: JaneTan
  Need to run 100+ Chrome’s with Selenium but can’t get past ~15 without breaking imillz 0 1,370 Sep-04-2021, 04:51 PM
Last Post: imillz
  Appropriate data-structure / design for business-day relations (week/month-wise) sx999 2 2,811 Apr-23-2021, 08:09 AM
Last Post: sx999
  Struggling for the past hour to define function and call it back godlyredwall 2 2,233 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  Make list of dates between today back to n days Mekala 3 2,402 Oct-03-2020, 01:01 PM
Last Post: ibreeden
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,245 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Learning Python in a month MuhammadNauman 1 1,777 Jul-17-2019, 11:53 AM
Last Post: metulburr
  How to get all the data for the current month in ms Access using python? aeo03 1 2,317 Nov-07-2018, 08:21 PM
Last Post: micseydel
  Creating folders dynamically with year/month/date sritsv 0 6,369 Oct-16-2017, 03:44 PM
Last Post: sritsv

Forum Jump:

User Panel Messages

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