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
#8
I have no desire to read through all that, but it looks like you've got two values, year and month, and you're incrementing the month.  You could check to see if the new value is over 12 and reset it to 1...
>>> dates = []
>>> year = 2016
>>> month = 1
>>> for i in range(20):
...   dates.append('{0:04d}{1:02d}'.format(year, month))
...   month += 1
...   if month > 12:
...     month = 1
...     year += 1
...
>>> dates
['201601', '201602', '201603', '201604', '201605', '201606', '201607', '201608', '201609', '201610', '201611', '201612', '201701', '201702', '201703', '201704', '201705', '201706', '201707', '201708']
>>>
Then there's also the datetime module and friends in case you don't want to do that math yourself.
Reply


Messages In This Thread
RE: Logic to convert an integer to YEAR / MONTH - by nilamo - Oct-17-2016, 04:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  get list of dates in past month, current and upcoming month jacksfrustration 4 603 Feb-03-2024, 06:37 PM
Last Post: deanhystad
  Trying to get year not the entire year & time mbrown009 2 914 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,051 Dec-12-2022, 12:49 AM
Last Post: JaneTan
  How to convert 4 bytes to an integer ? GiggsB 11 7,016 Jan-20-2022, 03:37 AM
Last Post: GiggsB
  Appropriate data-structure / design for business-day relations (week/month-wise) sx999 2 2,830 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,265 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,338 Aug-14-2020, 07:31 PM
Last Post: deanhystad
  Learning Python in a month MuhammadNauman 1 1,791 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,334 Nov-07-2018, 08:21 PM
Last Post: micseydel
Question [Help] Convert integer to Roman numerals? {Screenshot attached} vanicci 10 9,259 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