pythondatetimecalendarmonthcalendar

Get the dates of the previous month to appear instead of zeros in calendar python library


How do i get the dates of the previous month to appear like my desired output ? The iterator returns 0s in place of the previous/next month days that dont belong in the given month.


from calendar import Calendar

cal = Calendar()

cal.itermonthdays(year, month)
     November
Mo Tu We Th Fr Sa Su
0  0  1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 0  0  0


DESIRED OUTPUT

      November
Mo Tu We Th Fr Sa Su
30 31  1  2  3  4  5
 6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1  2  3


Solution

  • Listing [Python.Docs]: calendar - General calendar-related functions.

    There is more than one way (I found 2). Iterate over:

    code00.py:

    #!/usr/bin/env python
    
    import calendar as cal
    import sys
    
    
    def main(*argv):
        c = cal.Calendar()
        year = 2022
        months = (
            6,
            7,
            8,
            9,
        )
    
        for month in months:
            print("\n{:04d}/{:02d}:".format(year, month))
            print("{:s}: {:}".format(c.itermonthdays.__name__, list(c.itermonthdays(year, month))))
            print("{:s}: {:}".format(c.itermonthdates.__name__, [e.day for e in c.itermonthdates(year, month)]))
            print("{:s}: {:}".format(c.itermonthdays3.__name__, [e[-1] for e in c.itermonthdays3(year, month)]))
    
    
    if __name__ == "__main__":
        print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                       64 if sys.maxsize > 0x100000000 else 32, sys.platform))
        rc = main(*sys.argv[1:])
        print("\nDone.")
        sys.exit(rc)
    

    Output:

    [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q072898034]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" ./code00.py
    Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32
    
    
    2022/06:
    itermonthdays: [0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 0, 0, 0]
    itermonthdates: [30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3]
    itermonthdays3: [30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3]
    
    2022/07:
    itermonthdays: [0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
    itermonthdates: [27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
    itermonthdays3: [27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]
    
    2022/08:
    itermonthdays: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0]
    itermonthdates: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3, 4]
    itermonthdays3: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3, 4]
    
    2022/09:
    itermonthdays: [0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 0, 0]
    itermonthdates: [29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2]
    itermonthdays3: [29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2]
    
    Done.
    

    If caring about day of week as well (as required in a comment), itermonthdays3 and itermonthdays2 could be combined (via zip) together:

    >>> import calendar as cal
    >>>
    >>>
    >>> c = cal.Calendar()
    >>> year = 2022
    >>> month = 9
    >>>
    >>> [(e3[-1], e2[-1]) for e3, e2 in zip(c.itermonthdays3(year, month), c.itermonthdays2(year, month))]
    [(29, 0), (30, 1), (31, 2), (1, 3), (2, 4), (3, 5), (4, 6), (5, 0), (6, 1), (7, 2), (8, 3), (9, 4), (10, 5), (11, 6), (12, 0), (13, 1), (14, 2), (15, 3), (16, 4), (17, 5), (18, 6), (19, 0), (20, 1), (21, 2), (22, 3), (23, 4), (24, 5), (25, 6), (26, 0), (27, 1), (28, 2), (29, 3), (30, 4), (1, 5), (2, 6)]