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
#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


Messages In This Thread
RE: get list of dates in past month, current and upcoming month - by deanhystad - Jan-31-2024, 07:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why can't I copy and past only ONE specific tab? NewWorldRonin 8 947 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,073 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,407 Sep-04-2021, 04:51 PM
Last Post: imillz
  Appropriate data-structure / design for business-day relations (week/month-wise) sx999 2 2,864 Apr-23-2021, 08:09 AM
Last Post: sx999
  Struggling for the past hour to define function and call it back godlyredwall 2 2,277 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  Make list of dates between today back to n days Mekala 3 2,429 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,286 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Learning Python in a month MuhammadNauman 1 1,804 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,346 Nov-07-2018, 08:21 PM
Last Post: micseydel
  Creating folders dynamically with year/month/date sritsv 0 6,408 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