Python Forum
Logic to convert an integer to YEAR / MONTH
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Logic to convert an integer to YEAR / MONTH
#9
This is what I came up with.
from datetime import date

def yield_file_dates(start_year, start_month, amount_of_months):
    current_date = date(start_year, start_month, 1)
    for _ in range(amount_of_months):
        yield 'DC{:%Y%m}'.format(current_date)
        if current_date.month == 12:
            current_date = current_date.replace(current_date.year + 1, 1)
        else:
            current_date = current_date.replace(month=current_date.month + 1)

for file_date in yield_file_dates(2016, 10, 5):
    print(file_date)
Output:
DC201610 DC201611 DC201612 DC201701 DC201702
Reply


Messages In This Thread
RE: Logic to convert an integer to YEAR / MONTH - by Yoriz - Oct-17-2016, 05:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  get list of dates in past month, current and upcoming month jacksfrustration 4 699 Feb-03-2024, 06:37 PM
Last Post: deanhystad
  Trying to get year not the entire year & time mbrown009 2 985 Jan-09-2023, 01:46 PM
Last Post: snippsat
  Python Resampling: How do I obtain the value of the last week of the month? JaneTan 2 1,101 Dec-12-2022, 12:49 AM
Last Post: JaneTan
  How to convert 4 bytes to an integer ? GiggsB 11 7,418 Jan-20-2022, 03:37 AM
Last Post: GiggsB
  Appropriate data-structure / design for business-day relations (week/month-wise) sx999 2 2,907 Apr-23-2021, 08:09 AM
Last Post: sx999
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,327 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Help with implementation of the logic to convert roman numeral to integer jagasrik 2 2,383 Aug-14-2020, 07:31 PM
Last Post: deanhystad
  Learning Python in a month MuhammadNauman 1 1,832 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,380 Nov-07-2018, 08:21 PM
Last Post: micseydel
Question [Help] Convert integer to Roman numerals? {Screenshot attached} vanicci 10 9,407 Aug-06-2018, 05:19 PM
Last Post: vanicci

Forum Jump:

User Panel Messages

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