如何在两个日期之间找到天数以及星期五的数量 我如何在两个日期之间找到天数以及星期五的数量 在python中。蟒。
答案 0 :(得分:0)
两个日期之间的天数:
import datetime
start = datetime.datetime.strptime(raw_input('Enter date in format yyyy,mm,dd : '), '%Y,%m,%d')
end = datetime.datetime.strptime(raw_input('Enter date in format yyyy,mm,dd:'), '%Y,%m,%d')
diff = end-start
print diff.days
>> 361
获得星期五的数量:
# key 0 would be Monday as the start date is from Monday
days = {
0: 0,
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
}
full_weeks = (diff.days) / 7
remainder = (diff.days) % 7
first_day = start.weekday() # Start date is on Monday
for day in days.keys():
days[day] = full_weeks
for i in range(0, remainder):
days[(first_day + i) % 7] += 1
print days[4] # Gives number of Fridays between the date range
>> 2
Python文档 - 日期时间:https://docs.python.org/2/library/datetime.html