我想编写一个 python 函数来获取给定年份的月份开始和结束日期。
样本输入:2021
预期输出:
[(01-Jan-2021,31-Jan-2021), (01-Feb-2021,28-Feb-2021), (01-Mar-2021,31-Mar-2021), etc..)]
答案 0 :(得分:1)
结束日期在这里比较棘手,因为它可以是 28
、29
、30
或 31
。
您可以使用 isleap
模块中的 calendar
功能:
from calendar import isleap
import datetime
year = 2024
months_choices = []
for i in range(1, 13):
month = datetime.date(2021, i, 1).strftime('%b')
startDate = f"01-{month}-{year}"
if month in ["Jan", "Mar", "May", "Jul", "Aug", "Oct", "Dec"]:
endDate = f"31-{month}-{year}"
elif month in ["Apr", "Jun", "Sep", "Nov"]:
endDate = f"30-{month}-{year}"
else:
isLeap = isleap(1900)
if isLeap:
endDate = f"29-{month}-{year}"
else:
endDate = f"28-{month}-{year}"
months_choices.append((startDate, endDate))
print(months_choices)
输出(leap
年):
[('01-Jan-2024', '31-Jan-2024'), ('01-Feb-2024', '29-Feb-2024'), ('01-Mar-2024', '31-Mar-2024'), ('01-Apr-2024', '30-Apr-2024'), ('01-May-2024', '31-May-2024'), ('01-Jun-2024', '30-Jun-2024'), ('01-Jul-2024', '31-Jul-2024'), ('01-Aug-2024', '31-Aug-2024'), ('01-Sep-2024', '30-Sep-2024'), ('01-Oct-2024', '31-Oct-2024'), ('01-Nov-2024', '30-Nov-2024'), ('01-Dec-2024', '31-Dec-2024')]
答案 1 :(得分:1)
您实际上只是检查一年是否是一个飞跃,然后确定二月的长度。这可能是一种简单的方法:
import calendar
def MonthsStartAndEnd(year):
# create dictionary of normal month lengths
months = {
'Jan': '31',
'Feb': '28',
'Mar': '31',
'Apr': '30',
'May': '31',
'Jun': '30',
'Jul': '31',
'Aug': '31',
'Sep': '30',
'Oct': '31',
'Nov': '30',
'Dec': '31'
}
# check if the year is leap and if so update Feb to 29 days
if calendar.isleap(year):
months['Feb'] = '29'
# return the formatted list of values
return [f"01-{key}-{year},{value}-{key}-{year}" for key, value in months.items()]
print(MonthsStartAndEnd(2000))
print(MonthsStartAndEnd(2001))
#['01-Jan-2000,31-Jan-2000', '01-Feb-2000,29-Feb-2000', '01-Mar-2000,31-Mar-2000', '01-Apr-2000,31-Apr-2000', '01-May-2000,31-May-2000', '01-Jun-2000,31-Jun-2000', '01-Jul-2000,31-Jul-2000', '01-Aug-2000,31-Aug-2000', '01-Sep-2000,31-Sep-2000', '01-Oct-2000,31-Oct-2000', '01-Nov-2000,31-Nov-2000', '01-Dec-2000,31-Dec-2000']
['01-Jan-2001,31-Jan-2001', '01-Feb-2001,28-Feb-2001', '01-Mar-2001,31-Mar-2001', '01-Apr-2001,31-Apr-2001', '01-May-2001,31-May-2001', '01-Jun-2001,31-Jun-2001', '01-Jul-2001,31-Jul-2001', '01-Aug-2001,31-Aug-2001', '01-Sep-2001,31-Sep-2001', '01-Oct-2001,31-Oct-2001', '01-Nov-2001,31-Nov-2001', '01-Dec-2001,31-Dec-2001']
答案 2 :(得分:1)
您可以使用 pandas
date ranges,适当的 frequency alias 用于月份开始和月份结束。例如:
import pandas as pd
year, nMonths = "2020", 12
monthStart = pd.date_range(year, periods=nMonths, freq='MS').strftime("%d-%b-%Y")
monthEnd = pd.date_range(year, periods=nMonths, freq='M').strftime("%d-%b-%Y")
l = [(s,e) for s,e in zip(monthStart, monthEnd)]
l
[('01-Jan-2020', '31-Jan-2020'),
('01-Feb-2020', '29-Feb-2020'),
('01-Mar-2020', '31-Mar-2020'),
('01-Apr-2020', '30-Apr-2020'),
('01-May-2020', '31-May-2020'),
('01-Jun-2020', '30-Jun-2020'),
('01-Jul-2020', '31-Jul-2020'),
('01-Aug-2020', '31-Aug-2020'),
('01-Sep-2020', '30-Sep-2020'),
('01-Oct-2020', '31-Oct-2020'),
('01-Nov-2020', '30-Nov-2020'),
('01-Dec-2020', '31-Dec-2020')]