Python 查找两个日期范围之间的日期
Python Find a day dates between a range of two dates
我正在使用 python3 开发一个项目,其中我必须使用 api 并且需要获取日期范围内一周中特定日期的数据。
因此,示例输入参数为:
firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'
因此,日期将包含在 API 响应中,但当天没有任何信息。所以,我必须在日期范围内获取特定日期的所有日期,例如 Monday
。
根据上面的例子,如何获取firstDay
和lastDay
之间所有Mondays
的日期?
您是否尝试过使用 python 中的日期时间库,这是一个完美的场景。它包含有关年月日的所有信息,甚至可以计算日期。例如,查找两个日期之间相隔多少天。它还可以将日期转换成你想要的任何格式。
https://docs.python.org/3/library/datetime.html
这是对您问题的回答
from datetime import date, timedelta
dstart = datetime.strptime('1-January-2000', '%d-%B-%Y')
dend= datetime.strptime('22-February-2000', '%d-%B-%Y')
ans = (dend - dstart).days / 7
if int(ans) + 2/7 <= ans:
ans = int(ans) + 1
else:
ans = int(ans)
print(ans)
from datetime import date, timedelta, datetime
import time
firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'
firstDay = datetime.strptime(firstDay, '%d-%B-%Y')
lastDay = datetime.strptime(lastDay, '%d-%B-%Y')
dates = [firstDay + timedelta(days=x) for x in range((lastDay-firstDay).days + 1) if (firstDay + timedelta(days=x)).weekday() == time.strptime(weekDay, '%A').tm_wday]
产出
dates
[datetime.datetime(2000, 1, 3, 0, 0),
datetime.datetime(2000, 1, 10, 0, 0),
datetime.datetime(2000, 1, 17, 0, 0),
datetime.datetime(2000, 1, 24, 0, 0),
datetime.datetime(2000, 1, 31, 0, 0),
datetime.datetime(2000, 2, 7, 0, 0),
datetime.datetime(2000, 2, 14, 0, 0),
datetime.datetime(2000, 2, 21, 0, 0)]
输出格式为weekDay-Month-year
[d.strftime("%A-%B-%Y") for d in dates]
['Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-February-2000',
'Monday-February-2000',
'Monday-February-2000']
以firstDay
格式输出:1-January-2000
[d.strftime("%-d-%B-%Y") for d in dates]
['3-January-2000',
'10-January-2000',
'17-January-2000',
'24-January-2000',
'31-January-2000',
'7-February-2000',
'14-February-2000',
'21-February-2000']
请试试这个:
import datetime
import calendar
def get_all_weekdays_between_range(firstDay, lastDay, weekDay):
month_name_to_num = {v.lower(): k for k, v in enumerate(calendar.month_name)}
day_name_to_num = {v.lower(): k for k, v in enumerate(calendar.day_name)}
def parse_date(dateStr):
parts = dateStr.split('-')
return datetime.date(int(parts[2]), month_name_to_num[parts[1].lower()], int(parts[0]))
first = parse_date(firstDay)
last = parse_date(lastDay)
result = []
current = first
while current <= last:
if calendar.weekday(current.year, current.month, current.day) == day_name_to_num[weekDay.lower()]:
result.append(current)
current += datetime.timedelta(days=1)
return result
firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'
all_days = get_all_weekdays_between_range(firstDay,lastDay,weekDay)
for day in all_days:
print(day)
我正在使用 python3 开发一个项目,其中我必须使用 api 并且需要获取日期范围内一周中特定日期的数据。
因此,示例输入参数为:
firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'
因此,日期将包含在 API 响应中,但当天没有任何信息。所以,我必须在日期范围内获取特定日期的所有日期,例如 Monday
。
根据上面的例子,如何获取firstDay
和lastDay
之间所有Mondays
的日期?
您是否尝试过使用 python 中的日期时间库,这是一个完美的场景。它包含有关年月日的所有信息,甚至可以计算日期。例如,查找两个日期之间相隔多少天。它还可以将日期转换成你想要的任何格式。
https://docs.python.org/3/library/datetime.html
这是对您问题的回答
from datetime import date, timedelta
dstart = datetime.strptime('1-January-2000', '%d-%B-%Y')
dend= datetime.strptime('22-February-2000', '%d-%B-%Y')
ans = (dend - dstart).days / 7
if int(ans) + 2/7 <= ans:
ans = int(ans) + 1
else:
ans = int(ans)
print(ans)
from datetime import date, timedelta, datetime
import time
firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'
firstDay = datetime.strptime(firstDay, '%d-%B-%Y')
lastDay = datetime.strptime(lastDay, '%d-%B-%Y')
dates = [firstDay + timedelta(days=x) for x in range((lastDay-firstDay).days + 1) if (firstDay + timedelta(days=x)).weekday() == time.strptime(weekDay, '%A').tm_wday]
产出
dates
[datetime.datetime(2000, 1, 3, 0, 0),
datetime.datetime(2000, 1, 10, 0, 0),
datetime.datetime(2000, 1, 17, 0, 0),
datetime.datetime(2000, 1, 24, 0, 0),
datetime.datetime(2000, 1, 31, 0, 0),
datetime.datetime(2000, 2, 7, 0, 0),
datetime.datetime(2000, 2, 14, 0, 0),
datetime.datetime(2000, 2, 21, 0, 0)]
输出格式为weekDay-Month-year
[d.strftime("%A-%B-%Y") for d in dates]
['Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-January-2000',
'Monday-February-2000',
'Monday-February-2000',
'Monday-February-2000']
以firstDay
格式输出:1-January-2000
[d.strftime("%-d-%B-%Y") for d in dates]
['3-January-2000',
'10-January-2000',
'17-January-2000',
'24-January-2000',
'31-January-2000',
'7-February-2000',
'14-February-2000',
'21-February-2000']
请试试这个:
import datetime
import calendar
def get_all_weekdays_between_range(firstDay, lastDay, weekDay):
month_name_to_num = {v.lower(): k for k, v in enumerate(calendar.month_name)}
day_name_to_num = {v.lower(): k for k, v in enumerate(calendar.day_name)}
def parse_date(dateStr):
parts = dateStr.split('-')
return datetime.date(int(parts[2]), month_name_to_num[parts[1].lower()], int(parts[0]))
first = parse_date(firstDay)
last = parse_date(lastDay)
result = []
current = first
while current <= last:
if calendar.weekday(current.year, current.month, current.day) == day_name_to_num[weekDay.lower()]:
result.append(current)
current += datetime.timedelta(days=1)
return result
firstDay = '1-January-2000'
lastDay = '22-february-2000'
weekDay = 'Monday'
all_days = get_all_weekdays_between_range(firstDay,lastDay,weekDay)
for day in all_days:
print(day)