想象一下,你有一系列日期,例如2017-08-01 - 2017-09-15,以及开始日(星期一 - 数字格式1-7的sonday)和endDay。你必须计算这几天的组合数。 对于输入4-7当天是什么 - sonday和提到的日期输出将是6.你会怎么做?
答案 0 :(得分:0)
在您的示例中,您将拥有以下值:
rangeStart = 2017-08-01
rangeEnd = 2017-09-15
startDay = 4
endDay = 7
我想你也有以下功能:
weekday(date)
确定给定日期的星期几,并将其作为数字返回:1表示星期一,... 7表示星期日。date_add(date, days)
将天天添加到指定的日期并返回结果日期。 date_diff(date1, date2)
返回两个日期之间的天数,不包括 date2 本身,即当两个日期属于同一工作日时,这将是7的倍数。然后这可能是算法:
# Align range start with closest start day in the range
rangeStart = date_add(rangeStart, (startDay + 7 - weekday(rangeStart)) % 7)
# Align range end with closest end day within the range
rangeEnd = date_add(rangeEnd, -((weekday(rangeEnd) + 7 - endDay) % 7))
# Get number of full weeks in range, and add 1
result = floor(date_diff(rangeStart, rangeEnd) / 7) + 1
注意:%
是模运算符,floor
将十进制数截断为最接近的整数值。
在JavaScript中实现:
// Define utility functions whose implementations depend on the programming language
function weekday(date) {
return (date.getDay()+6)%7+1; // in JavaScript Sunday is 0
}
function date_add(date, days) {
var result = new Date(date); // get clone
result.setDate(result.getDate() + days); // mutate
return result;
}
function date_diff(date1, date2) {
return Math.round((date2-date1)/(1000*60*60*24));
}
var floor = Math.floor;
// Main algorithm
function count(rangeStart, rangeEnd, startDay, endDay) {
rangeStart = date_add(rangeStart, (startDay + 7 - weekday(rangeStart)) % 7);
rangeEnd = date_add(rangeEnd, -((weekday(rangeEnd) + 7 - endDay) % 7));
return floor(date_diff(rangeStart, rangeEnd) / 7) + 1;
}
var rangeStart = new Date("2017-08-01"),
rangeEnd = new Date("2017-09-15"),
startDay = 4,
endDay = 7;
var result = count(rangeStart, rangeEnd, startDay, endDay);
console.log(result);
在Python中实现:
import datetime
def count(rangeStart, rangeEnd, startDay, endDay):
rangeStart = rangeStart + datetime.timedelta((startDay + 7 - rangeStart.isoweekday()) % 7)
rangeEnd = rangeEnd - datetime.timedelta((rangeEnd.isoweekday() + 7 - endDay) % 7)
return (rangeEnd - rangeStart).days // 7 + 1
rangeStart = datetime.date(2017, 8, 3)
rangeEnd = datetime.date(2017, 9, 15)
startDay = 4
endDay = 7
result = count(rangeStart, rangeEnd, startDay, endDay)
print(result)