获取一周中指定两天之间的天数

时间:2019-05-30 23:16:45

标签: javascript arrays slice

我想找出一周中两天之间有多少天。

对于执行良好的slice方法而言,这似乎是一种情况;但我只是无法理解。

const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']

let today = 'Thursday'
let dispatch = 'Monday'
let remaining = '???'

console.log(`${remaining} Days until the next dispatch`)

周一至周四很容易:

let remaining = days.slice(days.indexOf('Monday'), days.indexOf('Thursday')).length + 1

但是,例如,星期四至星期一需要一些我不知道的逻辑。

5 个答案:

答案 0 :(得分:5)

您可以删除slice和if并使用一些模运算。

const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']

function getDays(start, end) {
    return (days.indexOf(end) + days.length - days.indexOf(start)) % days.length + 1;
}
console.log(getDays("Monday", "Thursday")); // 4
console.log(getDays("Thursday", "Monday")); // 5

如果您决定将星期一至星期四设为3而不是4(根据情况可能更自然),则从末尾开始+1。

答案 1 :(得分:1)

我认为您可以直接减去两个.indexOf()值而无需调用.slice(),不是吗?像这样:

const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']

let today = 'Thursday'
let dispatch = 'Monday'
let remaining = days.indexOf(dispatch) - days.indexOf(today) + 1
if (remaining < 1) {
    remaining += days.length
}

console.log(`${remaining} Days until the next dispatch`)

答案 2 :(得分:0)

如果顺序相反,则可以reverse数组。

const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']

let today = 'Thursday';
let dispatch = 'Monday';
let reversed, remaining;
if (days.indexOf(today) > days.indexOf(dispatch)) {
  reversed = days.slice().reverse();
  remaining = reversed.slice(reversed.indexOf(today), reversed.indexOf(dispatch)).length + 1;
} else {
  remaining = days.slice(days.indexOf(today), days.indexOf(dispatch)).length + 1;
}

console.log(`${remaining} Days until the next dispatch`)

答案 3 :(得分:0)

您不必使用slice函数,这是实现目标的一种方法:

const getRemaining = (arr, start, end) => {
  let startIndex = arr.indexOf(start);
  let endIndex = days.indexOf(end);
  if (startIndex > endIndex) {
    // We should go to beginning
    endIndex += arr.length;
  }
  return endIndex - startIndex;
};

const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

console.log('Thursday to Monday: ', getRemaining(days, 'Thursday', 'Monday'));
console.log('Monday to Thursday: ', getRemaining(days, 'Monday', 'Thursday'));

答案 4 :(得分:0)

类似的事情会起作用:

const days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']

let getDayDistance = (days, from, to) => {
  let f = days.indexOf(from), t = days.indexOf(to)
  return f < t ? (t - f) : (days.length - f) + t 
}

console.log(getDayDistance(days, 'Monday', 'Thursday'))   // 3
console.log(getDayDistance(days, 'Thursday', 'Monday'))   // 4
console.log(getDayDistance(days, 'Sunday', 'Saturday'))   // 6
console.log(getDayDistance(days, 'Saturday', 'Friday'))   // 6
console.log(getDayDistance(days, 'Thursday', 'Tuesday'))  // 5 
console.log(getDayDistance(days, 'Wednesday', 'Sunday'))  // 4
console.log(getDayDistance(days, 'Friday', 'Tuesday'))    // 4