【问题标题】:Generate list of months between interval in python在python中生成间隔之间的月份列表
【发布时间】:2016-01-20 11:16:02
【问题描述】:

我想生成一个 python 列表,其中包含两个日期之间发生的所有月份,输入和输出格式如下:

date1 = "2014-10-10"  # input start date
date2 = "2016-01-07"  # input end date
month_list = ['Oct-14', 'Nov-14', 'Dec-14', 'Jan-15', 'Feb-15', 'Mar-15', 'Apr-15', 'May-15', 'Jun-15', 'Jul-15', 'Aug-15', 'Sep-15', 'Oct-15', 'Nov-15', 'Dec-15', 'Jan-16']  # output

【问题讨论】:

  • 见我的回答here 的想法。您可以在那里修改函数以满足您的需要。

标签: python python-2.7


【解决方案1】:

我找到了一种非常简洁的方法来使用 Pandas 进行此操作,分享以防万一:


更新:this post 的帮助下,我已经把它归结为一条线:)

pd.date_range('2014-10-10','2016-01-07', 
              freq='MS').strftime("%Y-%b").tolist()

旧答案:

daterange = pd.date_range('2014-10-10','2016-01-07' , freq='1M') 
daterange = daterange.union([daterange[-1] + 1])  
daterange = [d.strftime('%y-%b') for d in daterange]

第二行防止最后一个日期从列表中删除。

【讨论】:

  • 我意识到更新版本与 tmsss 的回答非常相似......见下文。
【解决方案2】:
>>> from datetime import datetime, timedelta
>>> from collections import OrderedDict
>>> dates = ["2014-10-10", "2016-01-07"]
>>> start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
>>> OrderedDict(((start + timedelta(_)).strftime(r"%b-%y"), None) for _ in xrange((end - start).days)).keys()
['Oct-14', 'Nov-14', 'Dec-14', 'Jan-15', 'Feb-15', 'Mar-15', 'Apr-15', 'May-15', 'Jun-15', 'Jul-15', 'Aug-15', 'Sep-15', 'Oct-15', 'Nov-15', 'Dec-15', 'Jan-16']

更新: 一些解释,在一条评论中要求。这里存在三个问题:将日期解析为适当的数据结构(strptime);在给定两个极端和步骤(一个月)的情况下获取日期范围;格式化输出日期 (strftime)。 datetime 类型重载了减法运算符,因此 end - start 有意义。结果是一个 timedelta 对象,它表示两个日期之间的差异,.days 属性获取以天为单位的差异。没有.months 属性,因此我们一次迭代一天并将日期转换为所需的输出格式。这会产生很多重复项,OrderedDict 会删除这些重复项,同时保持项目的正确顺序。

现在这很简单,因为它让 datetime 模块完成所有工作,但它也非常低效。我们每天都调用很多方法,而我们只需要输出月份。如果性能不是问题,上面的代码就可以了。否则,我们将不得不多做一些工作。让我们将上述实现与更高效的实现进行比较:

from datetime import datetime, timedelta
from collections import OrderedDict

dates = ["2014-10-10", "2016-01-07"]

def monthlist_short(dates):
    start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
    return OrderedDict(((start + timedelta(_)).strftime(r"%b-%y"), None) for _ in xrange((end - start).days)).keys()

def monthlist_fast(dates):
    start, end = [datetime.strptime(_, "%Y-%m-%d") for _ in dates]
    total_months = lambda dt: dt.month + 12 * dt.year
    mlist = []
    for tot_m in xrange(total_months(start)-1, total_months(end)):
        y, m = divmod(tot_m, 12)
        mlist.append(datetime(y, m+1, 1).strftime("%b-%y"))
    return mlist

assert monthlist_fast(dates) == monthlist_short(dates)

if __name__ == "__main__":
    from timeit import Timer
    for func in "monthlist_short", "monthlist_fast":
        print func, Timer("%s(dates)" % func, "from __main__ import dates, %s" % func).timeit(1000)

在我的笔记本电脑上,我得到以下输出:

monthlist_short 2.3209939003
monthlist_fast 0.0774540901184

简洁的实现大约慢了 30 倍,所以我不建议在时间关键的应用程序中使用它:)

【讨论】:

  • 看看 OP 的预期输出
  • 对不起,我第一次读到斋戒。我已经修改了答案以实际解决问题:)
  • 简直太棒了..正是我要找的。如果你也解释这些东西会更好,以便其他观众也能清楚地理解它@simleo
  • 很好的解决方案。我一直在寻找这样的东西一段时间。许多其他解决方案并没有给我想要的结果。 Tnx!
  • 很好的解决方案,为我节省了大量时间!
【解决方案3】:

使用 pandas,您可以拥有这样的单行:

import pandas as pd

date1 = "2014-10-10"  # input start date
date2 = "2016-01-07"  # input end date

month_list = [i.strftime("%b-%y") for i in pd.date_range(start=date1, end=date2, freq='MS')]

【讨论】:

    【解决方案4】:

    你必须使用CalendarDatetime

    import calendar
    from datetime import *
    date1 = datetime.strptime("2014-10-10", "%Y-%m-%d")
    date2 = datetime.strptime("2016-01-07", "%Y-%m-%d")
    date1 = date1.replace(day = 1)
    date2 = date2.replace(day = 1)
    months_str = calendar.month_name
    months = []
    while date1 < date2:
        month = date1.month
        year  = date1.year
        month_str = months_str[month][0:3]
        months.append("{0}-{1}".format(month_str,str(year)[-2:]))
        next_month = month+1 if month != 12 else 1
        next_year = year + 1 if next_month == 1 else year
        date1 = date1.replace( month = next_month, year= next_year)
    
    print months
    

    此代码返回

    ['Oct-14', 'Nov-14', 'Dec-14', 'Jan-14', 'Feb-15', 'Mar-15', 'Apr-15', 'May-15', 'Jun-15', 'Jul-15', 'Aug-15', 'Sep-15', 'Oct-15', 'Nov-15', 'Dec-15', 'Jan-15']
    

    【讨论】:

    • 我通过运行你的代码得到这个:['Oct-1', 'Nov-1', 'Dec-1', 'Jan-1', 'Feb-1', 'Mar- 1'、'Apr-1'、'May-1'、'Jun-1'、'Jul-1'、'Aug-1'、'Sep-1'、'Oct-1'、'Nov-1' , 'Dec-1', 'Jan-1']
    • 是的!这是一个小错误。我写了str(year)[-2]而不是str(year)[-2:]。固定的! :)
    【解决方案5】:

    如果您有兴趣将日期保存为 Python 格式,可以尝试使用 to_pydatetime()

    import pandas as pd
    from datetime import datetime
    
    datemin = datetime(2010, 1, 1)
    datemax = datetime(2019, 12, 31)
    
    # First day of month
    pd.date_range(datemin, datemax, freq='MS').to_pydatetime().tolist()
    
    # Last day of month
    pd.date_range(datemin, datemax, freq='M').to_pydatetime().tolist()
    

    List of Offset Aliases

    【讨论】:

      【解决方案6】:

      这是我的解决方案,其中包含一个简单的列表理解,它使用 range 知道月份必须从哪里开始和结束

      from datetime import datetime as dt
      sd = dt.strptime('2014-10-10', "%Y-%m-%d") 
      ed = dt.strptime('2016-01-07', "%Y-%m-%d") 
      
      lst = [dt.strptime('%2.2d-%2.2d' % (y, m), '%Y-%m').strftime('%b-%y') \
             for y in xrange(sd.year, ed.year+1) \
             for m in xrange(sd.month if y==sd.year else 1, ed.month+1 if y == ed.year else 13)]
      
      print lst
      

      生产

      ['Oct-14', 'Nov-14', 'Dec-14', 'Jan-15', 'Feb-15', 'Mar-15', 'Apr-15', 'May-15', 'Jun-15', 'Jul-15', 'Aug-15', 'Sep-15', 'Oct-15', 'Nov-15', 'Dec-15', 'Jan-16']
      

      【讨论】:

        【解决方案7】:

        在下面找到我使用 split 和简单的基于模 迭代而不导入任何特殊模块的方法来解决这个问题。

        date1 = "2014-10-10"
        date2 = "2016-01-07"
        
        y0 = int( date1.split('-')[0] ) # 2014
        y1 = int( date2.split('-')[0] ) # 2016
        
        m0 = int( date1.split('-')[1] ) - 1 # 10-1 --> 9 because will be used for indexing
        m1 = int( date2.split('-')[1] ) - 1 # 01-1 --> 0 because will be used for indexing
        
        months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        result = []
        start = m0
        for y in range(y0, y1+1):
            for m in range(start,12):
                result.append( str( months[m  % 12])+'-'+str(y) )
                if y == y1 and (m % 12) == m1:
                    break
            start = 0
        
        print result
        

        $ python dates.py

        ['Oct-2014', 'Nov-2014', 'Dec-2014', 'Jan-2015', 'Feb-2015', 'Mar-2015', 'Apr-2015', 'May-2015', 'Jun-2015', 'Jul-2015', 'Aug-2015', 'Sep-2015', 'Oct-2015', 'Nov-2015', 'Dec-2015', 'Jan-2016']
        

        【讨论】:

          【解决方案8】:

          以前做过类似的事情,我尝试解决这个问题。使用不同的组件来执行此操作更加灵活,并且使您能够针对不同的用例混合和匹配它们。也可以通过这种方式更轻松地测试它们,正如您在 iterate_months 中的文档测试中看到的那样。

          我还建议使用datetime.date 对象作为您的输入,因为您可以使用这些对象做更多事情。为此,您必须首先解析您的输入字符串,但这很容易做到。

          解析日期字符串

          def datify(date):
              if isinstance(date, datetime.date):
                  return date
              elif isinstance(date, datetime.datetime):
                  return date.date()
              else:
                  # taken from simleo's answer
                  return datetime.strptime(date, "%Y-%m-%d")
          

          首先,我们遍历月份

          import datetime
          
          
          def iterate_months(start_date, end_date):
              """Iterate monthly between two given dates.
          
              Emitted will be the first day of each month.
          
              >>> list(iterate_months(datetime.date(1999, 11, 1),
              ...                     datetime.date(2000, 2, 1)))
              [datetime.date(1999, 11, 1), datetime.date(1999, 12, 1),\
           datetime.date(2000, 1, 1), datetime.date(2000, 2, 1)]
          
              """
              assert isinstance(start_date, datetime.date)
              assert isinstance(end_date, datetime.date)
              assert start_date < end_date
          
              year = start_date.year
              month = start_date.month
              while True:
                  current = datetime.date(year, month, 1)
                  yield current
                  if current.month == end_date.month and current.year == end_date.year:
                      break
                  else:
                      month = ((month + 1) % 12) or 12
                      if month == 1:
                          year += 1
          
          
          if __name__ == '__main__':
              import doctest
              doctest.testmod()
          

          要格式化您的日期,请使用类似这样的内容

          def format_month(date):
              return date.strftime(r"%b-%y")
          

          把它们放在一起

          start = datify("2014-10-10")
          end = datify("2016-01-07")
          
          for entry in iterate_months(start, end):
              print format_month(entry)
          

          或将其保存为列表:

          result = list(iterate_months(start, end))
          

          【讨论】:

            【解决方案9】:

            如果您想要一个字典,其中包含您想要的年份之间的月份开始日期和结束日期,那么这里是如何获得它。

            start_year = int(input("Enter Starting Year: "))
            end_year = int(input("Enter Ending Year: "))
            import calendar
            month_dict = {}
            for x in range(start_year, end_year):
                for y in range(1, 13):
                    if y < 10:
                        start_date = '01'+'-'+'0'+str(y)+'-'+str(x)
                        end_date = str(calendar.monthrange(x, y)[1])+'-'+'0'+str(y)+'-'+str(x)
                        month_dict[start_date] = end_date
                    else:
                        start_date = '01'+'-'+str(y)+'-'+str(x)
                        end_date = str(calendar.monthrange(x, y)[1])+'-'+str(y)+'-'+str(x)
                        month_dict[start_date] = end_date
            

            这给出了以下输出:

            {'01-01-2000': '31-01-2000',
             '01-02-2000': '29-02-2000',
             '01-03-2000': '31-03-2000',
             '01-04-2000': '30-04-2000',
             '01-05-2000': '31-05-2000',
             '01-06-2000': '30-06-2000',
             '01-07-2000': '31-07-2000',
             '01-08-2000': '31-08-2000',
             '01-09-2000': '30-09-2000',
             '01-10-2000': '31-10-2000',
             '01-11-2000': '30-11-2000',
             '01-12-2000': '31-12-2000',
             '01-01-2001': '31-01-2001',
             '01-02-2001': '28-02-2001',
             '01-03-2001': '31-03-2001',
             '01-04-2001': '30-04-2001',
             '01-05-2001': '31-05-2001',
             '01-06-2001': '30-06-2001',
             '01-07-2001': '31-07-2001',
             '01-08-2001': '31-08-2001',
             '01-09-2001': '30-09-2001',
             '01-10-2001': '31-10-2001',
             '01-11-2001': '30-11-2001',
             '01-12-2001': '31-12-2001',
             '01-01-2002': '31-01-2002',
             '01-02-2002': '28-02-2002',
             '01-03-2002': '31-03-2002',
             '01-04-2002': '30-04-2002',
             '01-05-2002': '31-05-2002',
             '01-06-2002': '30-06-2002',
             '01-07-2002': '31-07-2002',
             '01-08-2002': '31-08-2002',
             '01-09-2002': '30-09-2002',
             '01-10-2002': '31-10-2002',
             '01-11-2002': '30-11-2002',
             '01-12-2002': '31-12-2002',
             '01-01-2003': '31-01-2003',
             '01-02-2003': '28-02-2003',
             '01-03-2003': '31-03-2003',
             '01-04-2003': '30-04-2003',
             '01-05-2003': '31-05-2003',
             '01-06-2003': '30-06-2003',
             '01-07-2003': '31-07-2003',
             '01-08-2003': '31-08-2003',
             '01-09-2003': '30-09-2003',
             '01-10-2003': '31-10-2003',
             '01-11-2003': '30-11-2003',
             '01-12-2003': '31-12-2003',
             '01-01-2004': '31-01-2004',
             '01-02-2004': '29-02-2004',
             '01-03-2004': '31-03-2004',
             '01-04-2004': '30-04-2004',
             '01-05-2004': '31-05-2004',
             '01-06-2004': '30-06-2004',
             '01-07-2004': '31-07-2004',
             '01-08-2004': '31-08-2004',
             '01-09-2004': '30-09-2004',
             '01-10-2004': '31-10-2004',
             '01-11-2004': '30-11-2004',
             '01-12-2004': '31-12-2004',
             '01-01-2005': '31-01-2005',
             '01-02-2005': '28-02-2005',
             '01-03-2005': '31-03-2005',
             '01-04-2005': '30-04-2005',
             '01-05-2005': '31-05-2005',
             '01-06-2005': '30-06-2005',
             '01-07-2005': '31-07-2005',
             '01-08-2005': '31-08-2005',
             '01-09-2005': '30-09-2005',
             '01-10-2005': '31-10-2005',
             '01-11-2005': '30-11-2005',
             '01-12-2005': '31-12-2005',
             '01-01-2006': '31-01-2006',
             '01-02-2006': '28-02-2006',
             '01-03-2006': '31-03-2006',
             '01-04-2006': '30-04-2006',
             '01-05-2006': '31-05-2006',
             '01-06-2006': '30-06-2006',
             '01-07-2006': '31-07-2006',
             '01-08-2006': '31-08-2006',
             '01-09-2006': '30-09-2006',
             '01-10-2006': '31-10-2006',
             '01-11-2006': '30-11-2006',
             '01-12-2006': '31-12-2006',
             '01-01-2007': '31-01-2007',
             '01-02-2007': '28-02-2007',
             '01-03-2007': '31-03-2007',
             '01-04-2007': '30-04-2007',
             '01-05-2007': '31-05-2007',
             '01-06-2007': '30-06-2007',
             '01-07-2007': '31-07-2007',
             '01-08-2007': '31-08-2007',
             '01-09-2007': '30-09-2007',
             '01-10-2007': '31-10-2007',
             '01-11-2007': '30-11-2007',
             '01-12-2007': '31-12-2007',
             '01-01-2008': '31-01-2008',
             '01-02-2008': '29-02-2008',
             '01-03-2008': '31-03-2008',
             '01-04-2008': '30-04-2008',
             '01-05-2008': '31-05-2008',
             '01-06-2008': '30-06-2008',
             '01-07-2008': '31-07-2008',
             '01-08-2008': '31-08-2008',
             '01-09-2008': '30-09-2008',
             '01-10-2008': '31-10-2008',
             '01-11-2008': '30-11-2008',
             '01-12-2008': '31-12-2008',
             '01-01-2009': '31-01-2009',
             '01-02-2009': '28-02-2009',
             '01-03-2009': '31-03-2009',
             '01-04-2009': '30-04-2009',
             '01-05-2009': '31-05-2009',
             '01-06-2009': '30-06-2009',
             '01-07-2009': '31-07-2009',
             '01-08-2009': '31-08-2009',
             '01-09-2009': '30-09-2009',
             '01-10-2009': '31-10-2009',
             '01-11-2009': '30-11-2009',
             '01-12-2009': '31-12-2009',
             '01-01-2010': '31-01-2010',
             '01-02-2010': '28-02-2010',
             '01-03-2010': '31-03-2010',
             '01-04-2010': '30-04-2010',
             '01-05-2010': '31-05-2010',
             '01-06-2010': '30-06-2010',
             '01-07-2010': '31-07-2010',
             '01-08-2010': '31-08-2010',
             '01-09-2010': '30-09-2010',
             '01-10-2010': '31-10-2010',
             '01-11-2010': '30-11-2010',
             '01-12-2010': '31-12-2010',
             '01-01-2011': '31-01-2011',
             '01-02-2011': '28-02-2011',
             '01-03-2011': '31-03-2011',
             '01-04-2011': '30-04-2011',
             '01-05-2011': '31-05-2011',
             '01-06-2011': '30-06-2011',
             '01-07-2011': '31-07-2011',
             '01-08-2011': '31-08-2011',
             '01-09-2011': '30-09-2011',
             '01-10-2011': '31-10-2011',
             '01-11-2011': '30-11-2011',
             '01-12-2011': '31-12-2011',
             '01-01-2012': '31-01-2012',
             '01-02-2012': '29-02-2012',
             '01-03-2012': '31-03-2012',
             '01-04-2012': '30-04-2012',
             '01-05-2012': '31-05-2012',
             '01-06-2012': '30-06-2012',
             '01-07-2012': '31-07-2012',
             '01-08-2012': '31-08-2012',
             '01-09-2012': '30-09-2012',
             '01-10-2012': '31-10-2012',
             '01-11-2012': '30-11-2012',
             '01-12-2012': '31-12-2012',
             '01-01-2013': '31-01-2013',
             '01-02-2013': '28-02-2013',
             '01-03-2013': '31-03-2013',
             '01-04-2013': '30-04-2013',
             '01-05-2013': '31-05-2013',
             '01-06-2013': '30-06-2013',
             '01-07-2013': '31-07-2013',
             '01-08-2013': '31-08-2013',
             '01-09-2013': '30-09-2013',
             '01-10-2013': '31-10-2013',
             '01-11-2013': '30-11-2013',
             '01-12-2013': '31-12-2013',
             '01-01-2014': '31-01-2014',
             '01-02-2014': '28-02-2014',
             '01-03-2014': '31-03-2014',
             '01-04-2014': '30-04-2014',
             '01-05-2014': '31-05-2014',
             '01-06-2014': '30-06-2014',
             '01-07-2014': '31-07-2014',
             '01-08-2014': '31-08-2014',
             '01-09-2014': '30-09-2014',
             '01-10-2014': '31-10-2014',
             '01-11-2014': '30-11-2014',
             '01-12-2014': '31-12-2014',
             '01-01-2015': '31-01-2015',
             '01-02-2015': '28-02-2015',
             '01-03-2015': '31-03-2015',
             '01-04-2015': '30-04-2015',
             '01-05-2015': '31-05-2015',
             '01-06-2015': '30-06-2015',
             '01-07-2015': '31-07-2015',
             '01-08-2015': '31-08-2015',
             '01-09-2015': '30-09-2015',
             '01-10-2015': '31-10-2015',
             '01-11-2015': '30-11-2015',
             '01-12-2015': '31-12-2015',
             '01-01-2016': '31-01-2016',
             '01-02-2016': '29-02-2016',
             '01-03-2016': '31-03-2016',
             '01-04-2016': '30-04-2016',
             '01-05-2016': '31-05-2016',
             '01-06-2016': '30-06-2016',
             '01-07-2016': '31-07-2016',
             '01-08-2016': '31-08-2016',
             '01-09-2016': '30-09-2016',
             '01-10-2016': '31-10-2016',
             '01-11-2016': '30-11-2016',
             '01-12-2016': '31-12-2016',
             '01-01-2017': '31-01-2017',
             '01-02-2017': '28-02-2017',
             '01-03-2017': '31-03-2017',
             '01-04-2017': '30-04-2017',
             '01-05-2017': '31-05-2017',
             '01-06-2017': '30-06-2017',
             '01-07-2017': '31-07-2017',
             '01-08-2017': '31-08-2017',
             '01-09-2017': '30-09-2017',
             '01-10-2017': '31-10-2017',
             '01-11-2017': '30-11-2017',
             '01-12-2017': '31-12-2017',
             '01-01-2018': '31-01-2018',
             '01-02-2018': '28-02-2018',
             '01-03-2018': '31-03-2018',
             '01-04-2018': '30-04-2018',
             '01-05-2018': '31-05-2018',
             '01-06-2018': '30-06-2018',
             '01-07-2018': '31-07-2018',
             '01-08-2018': '31-08-2018',
             '01-09-2018': '30-09-2018',
             '01-10-2018': '31-10-2018',
             '01-11-2018': '30-11-2018',
             '01-12-2018': '31-12-2018',
             '01-01-2019': '31-01-2019',
             '01-02-2019': '28-02-2019',
             '01-03-2019': '31-03-2019',
             '01-04-2019': '30-04-2019',
             '01-05-2019': '31-05-2019',
             '01-06-2019': '30-06-2019',
             '01-07-2019': '31-07-2019',
             '01-08-2019': '31-08-2019',
             '01-09-2019': '30-09-2019',
             '01-10-2019': '31-10-2019',
             '01-11-2019': '30-11-2019',
             '01-12-2019': '31-12-2019',
             '01-01-2020': '31-01-2020',
             '01-02-2020': '29-02-2020',
             '01-03-2020': '31-03-2020',
             '01-04-2020': '30-04-2020',
             '01-05-2020': '31-05-2020',
             '01-06-2020': '30-06-2020',
             '01-07-2020': '31-07-2020',
             '01-08-2020': '31-08-2020',
             '01-09-2020': '30-09-2020',
             '01-10-2020': '31-10-2020',
             '01-11-2020': '30-11-2020',
             '01-12-2020': '31-12-2020',
             '01-01-2021': '31-01-2021',
             '01-02-2021': '28-02-2021',
             '01-03-2021': '31-03-2021',
             '01-04-2021': '30-04-2021',
             '01-05-2021': '31-05-2021',
             '01-06-2021': '30-06-2021',
             '01-07-2021': '31-07-2021',
             '01-08-2021': '31-08-2021',
             '01-09-2021': '30-09-2021',
             '01-10-2021': '31-10-2021',
             '01-11-2021': '30-11-2021',
             '01-12-2021': '31-12-2021'}
            

            【讨论】:

              【解决方案10】:

              我找到了一个使用python-dateutil 并适用于 Python 3.8+ 的解决方案:

              https://gist.github.com/anatoly-scherbakov/593770d446a06f109438a134863ba969

              def month_range(
                  start: datetime.date,
                  end: datetime.date,
              ) -> Iterator[datetime.date]:
                  """Yields the 1st day of each month in the given date range."""
                  yield from itertools.takewhile(
                      lambda date: date < end,
                      itertools.accumulate(
                          itertools.repeat(relativedelta(months=1)),
                          operator.add,
                          initial=start,
                      )
                  )
              

              【讨论】:

                【解决方案11】:

                这里是 Pynchia 建议的类似版本,下面的实现是针对 python 3.8 他实现的一个是针对 python 2.x

                import datetime
                st="2020-06-24"
                ed="2020-11-24"
                start_date = datetime.datetime.strptime(st.strip(), '%Y-%m-%d')
                end_date = datetime.datetime.strptime(ed.strip(), '%Y-%m-%d')
                
                months = [datetime.datetime.strptime('%2.2d-%2.2d' % (y, m), '%Y-%m').strftime('%b-%y')
                    for y in range(start_date.year, end_date.year + 1)
                    for m in range(start_date.month if y == start_date.year else 1, end_date.month + 1 if y == end_date.year else 13)]
                
                print(months)
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2012-05-28
                  • 2013-03-04
                  • 1970-01-01
                  • 2020-07-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多