首页 > 解决方案 > 如何使用 Rrule 或 python 中的任何其他 API 获取每周日期,它返回每个月开始、月中和月末的列表,而与输入日期无关

问题描述

基本上以下代码给出了按周日期如下。我参考了这个文档页面

2021-01-07
2021-01-21
2021-02-04
2021-02-18

from datetime import datetime
from dateutil.rrule import rrule, DAILY, WEEKLY, MONTHLY 
    
def bi_weekly(start_date=datetime.now(),count=53,interval=2):
    # returns the datetime for an year and calculates them for 1 By weekly
    return list(rrule(WEEKLY, count=count,interval=interval, dtstart=start_date))
################  Test.    
dateTImeSart = bi_weekly(datetime.strptime('2021-01-07', '%Y-%m-%d'), 53)
print(dateTImeSart[0].strftime("%Y-%m-%d"))
print(dateTImeSart[1].strftime("%Y-%m-%d"))
print(dateTImeSart[2].strftime("%Y-%m-%d"))
print(dateTImeSart[3].strftime("%Y-%m-%d"))
print(dateTImeSart[4].strftime("%Y-%m-%d"))
print(dateTImeSart[5].strftime("%Y-%m-%d"))
print(dateTImeSart[50].strftime("%Y-%m-%d"))
print(dateTImeSart[51].strftime("%Y-%m-%d"))
print(dateTImeSart[52].strftime("%Y-%m-%d"))
########### output 
          2021-01-07
          2021-01-21
          2021-02-04
          2021-02-18
          2021-03-04
          2021-03-18
          2022-12-08
          2022-12-22
          2023-01-05

但是,我正在寻找如下日期:(start -mid of the month- mid -end of the month, ...

2021-01-07 2021-01-16,  2021-01-16 2021-01-31, 2021-02-01 2021-02-14,
2021-02-14 2021-02-28,  2021-03-01 2021-03-16, 2021-03-16 2021-03-31
2021-04-01 2021-04-15,  2021-04-15 2021-04-30,....
or 
2021-01-07, 2021-01-16, 2021-01-31, 
2021-02-01, 2021-02-14, 2021-02-28,  
2021-03-01, 2021-03-16, 2021-03-31,
2021-04-01, 2021-04-15, 2021-04-30,....

从我每个月都知道的文档页面:

def monthly(start_date=datetime.now(),count=13,interval=1):
    # returns the datetime for Monthly on the first and last day of the month for given count.
    return list(rrule(MONTHLY, bymonthday=(-1,1), interval=1,count=count, dtstart=start_date))

它生成:

dateTImeSart = monthly(datetime.strptime('2020-01-11', '%Y-%m-%d'),13)
print(dateTImeSart[0].strftime("%Y-%m-%d"))
print(dateTImeSart[1].strftime("%Y-%m-%d"))
print(dateTImeSart[2].strftime("%Y-%m-%d"))
print(dateTImeSart[3].strftime("%Y-%m-%d"))
print(dateTImeSart[4].strftime("%Y-%m-%d"))
print(dateTImeSart[5].strftime("%Y-%m-%d"))
print(dateTImeSart[6].strftime("%Y-%m-%d"))
print(dateTImeSart[11].strftime("%Y-%m-%d"))
print(dateTImeSart[12].strftime("%Y-%m-%d"))

2020-01-31
2020-02-01
2020-02-29
2020-03-01
2020-03-31
2020-04-01
2020-04-30
2020-07-01
2020-07-31

标签: python-3.xdatetimepython-datetimerrule

解决方案


我设法实现如下代码。我参考这篇文章

如果任何其他选项或实施比以下内容更好,请分享您的意见:

from datetime import datetime
from dateutil.rrule import rrule, DAILY, WEEKLY, MONTHLY 

def monthly(start_date=datetime.now(),count=13,interval=1):
    """
      dateTImeSart = monthly(datetime.strptime('2020-01-07', '%Y-%m-%d'),13)
      print(dateTImeSart[0].strftime("%Y-%m-%d"))
      print(dateTImeSart[1].strftime("%Y-%m-%d"))
      print(dateTImeSart[2].strftime("%Y-%m-%d"))
      print(dateTImeSart[3].strftime("%Y-%m-%d"))
      print(dateTImeSart[4].strftime("%Y-%m-%d"))
      print(dateTImeSart[5].strftime("%Y-%m-%d"))
      print(dateTImeSart[6].strftime("%Y-%m-%d"))
      print(dateTImeSart[11].strftime("%Y-%m-%d"))
      print(dateTImeSart[12].strftime("%Y-%m-%d"))
      2020-01-07
      2020-01-31
      2020-02-01
      2020-02-29
      2020-03-01
      2020-03-31
      2020-04-01
      2020-06-30
      2020-07-01
    """
    # returns the datetime for an Monthly on the first and last day of the month for given count.
    returnList = list(rrule(MONTHLY, bymonthday=(-1,1), interval=1,count=count, dtstart=start_date))
    # if the given date is string of the month we dont need to insert it will be present in the list automatically. 
    if (len(returnList) > 0 and start_date!= returnList[0]):
      returnList.insert(0,start_date)
    return returnList;

def bi_weekly(startingDate=datetime.now(),count=52):
    """
    dateTImeSart = bi_weekly(datetime.strptime('2021-01-07', '%Y-%m-%d'), 52)
    print(dateTImeSart[0].strftime("%Y-%m-%d"))
    print(dateTImeSart[1].strftime("%Y-%m-%d"))
    print(dateTImeSart[2].strftime("%Y-%m-%d"))
    print(dateTImeSart[3].strftime("%Y-%m-%d"))
    print(dateTImeSart[4].strftime("%Y-%m-%d"))
    print(dateTImeSart[5].strftime("%Y-%m-%d"))
    print(dateTImeSart[22].strftime("%Y-%m-%d"))
    print(dateTImeSart[23].strftime("%Y-%m-%d"))
    print(dateTImeSart[24].strftime("%Y-%m-%d"))
    # Output :
    2021-01-07
    2021-01-19
    2021-01-31
    2021-02-01
    2021-02-14
    2021-02-28
    2021-08-16
    2021-08-31
    2021-09-01
    """
    montlyDateRangeList = monthly(startingDate,count/2)
    returnbyWeeklyList = []
    i = 0
    while (i < count/2-1):
      start = montlyDateRangeList[i]
      end = montlyDateRangeList[i+1]
      mid = start + (end - start)/2
      returnbyWeeklyList.append(start)
      returnbyWeeklyList.append(mid)
      returnbyWeeklyList.append(end)
      i = i+2
      #print(returnbyWeeklyList)
    #print(returnbyWeeklyList)
    return returnbyWeeklyList





# Test
print("-By_Weely-")
dateTImeSart = bi_weekly(datetime.strptime('2021-01-07', '%Y-%m-%d'), 52)
print(dateTImeSart[0].strftime("%Y-%m-%d"))
print(dateTImeSart[1].strftime("%Y-%m-%d"))
print(dateTImeSart[2].strftime("%Y-%m-%d"))
print(dateTImeSart[3].strftime("%Y-%m-%d"))
print(dateTImeSart[4].strftime("%Y-%m-%d"))
print(dateTImeSart[5].strftime("%Y-%m-%d"))
print(dateTImeSart[22].strftime("%Y-%m-%d"))
print(dateTImeSart[23].strftime("%Y-%m-%d"))
print(dateTImeSart[24].strftime("%Y-%m-%d"))
print("- monthly-")
dateTImeSart = monthly(datetime.strptime('2020-01-07', '%Y-%m-%d'),13)
print(dateTImeSart[0].strftime("%Y-%m-%d"))
print(dateTImeSart[1].strftime("%Y-%m-%d"))
print(dateTImeSart[2].strftime("%Y-%m-%d"))
print(dateTImeSart[3].strftime("%Y-%m-%d"))
print(dateTImeSart[4].strftime("%Y-%m-%d"))
print(dateTImeSart[5].strftime("%Y-%m-%d"))
print(dateTImeSart[6].strftime("%Y-%m-%d"))
print(dateTImeSart[11].strftime("%Y-%m-%d"))
print(dateTImeSart[12].strftime("%Y-%m-%d"))


-By_Weely-
2021-01-07
2021-01-19
2021-01-31
2021-02-01
2021-02-14
2021-02-28
2021-08-16
2021-08-31
2021-09-01
- monthly-
2020-01-07
2020-01-31
2020-02-01
2020-02-29
2020-03-01
2020-03-31
2020-04-01
2020-06-30
2020-07-01

推荐阅读