简体   繁体   English

moment.js 影响时间范围的天数

[英]Affected days of time range with moment.js

I try to loop through two given date time.我尝试遍历两个给定的日期时间。 I have to make some calculation on each day of the range.我必须对范围的每一天进行一些计算。 I made a JsBin to setup the problem.我做了一个JsBin来设置问题。

 const timeRange1 = ['2019-10-22 14:00:00', '2019-10-22 19:00:00']; const timeRange2 = ['2019-10-22 13:30:00', '2019-10-24 10:00:00']; const timeRange3 = ['2019-10-22 06:00:00', '2019-10-23 23:00:00']; const timeRange4 = ['2019-10-21 23:00:00', '2019-10-22 01:00:00']; function loop(range) { const rangeStart = moment(range[0]) const rangeEnd = moment(range[1]) let i = 0 for (let m = rangeStart; m.diff(rangeEnd, 'days') <= 0; m.add(1, 'days')) { i++ } return i } // Here you can see the expected values and the wrong results. console.log(loop(timeRange1)); // 1 - 2 console.log(loop(timeRange2)); // 3 - 3 console.log(loop(timeRange3)); // 2 - 3 console.log(loop(timeRange4)); // 2 - 2
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

This seems to come from the times.这似乎来自时代。 When the time part of the range end is higher than the one from the range start, adding a day does not make the diff lower than 0.当范围结束的时间部分高于范围开始的时间部分时,添加一天不会使差异低于 0。

If you truncate them from your moment values, the results are what you expect (use .startOf('day') )如果你从你的时刻值中截断它们,结果就是你所期望的(使用.startOf('day')

function loop(range) {
  const rangeStart = moment(range[0]).startOf('day')
  const rangeEnd = moment(range[1]).startOf('day')
  let i = 0
  for (let m = rangeStart; m.diff(rangeEnd, 'days') <= 0; m.add(1, 'days')) {
    i++
  }
  return i
}

The problem is that diff truncates to an integer, so the loop doesn't stop until the difference is at least 1 day.问题是diff被截断为 integer,因此循环在差异至少1 天之前不会停止。 Consider the first example:考虑第一个例子:

  1. rangeStart initial value: '2019-10-22 14:00:00' rangeStart 初始值:'2019-10-22 14:00:00'
  2. rangeEnd value: '2019-10-22 19:00:00' rangeEnd 值:'2019-10-22 19:00:00'

1st iteration:第一次迭代:

  1. difference: 0.2 days, which truncates to 0 so i is incremented (1)差异:0.2 天,截断为 0,因此i递增 (1)
  2. rangeStart incremented to: '2019-10-23 14:00:00' rangeStart 增加到:'2019-10-23 14:00:00'

2nd iteration:第二次迭代:

  1. difference is -0.8 days, which truncates to 0 so i is incremented again (2)差异为 -0.8 天,截断为 0,因此i再次递增 (2)
  2. rangeStart incremented to: '2019-10-24 14:00:00' rangeStart 增加到:'2019-10-24 14:00:00'

3rd iteration第三次迭代

  1. difference is -1.8 days, which truncates to -1, so the loop terminates差是-1.8天,截断为-1,所以循环终止
  2. i is returned (2)被退回 (2)

The comparison needs to either:比较需要:

  1. Account for the truncation考虑截断
  2. Use decimal days (use 3rd parameter of true)使用小数天数(使用 true 的第三个参数)
  3. Set the dates to the same time (eg 00:00:00 as suggested by guillaume.deslandes).将日期设置为相同的时间(例如 guillaume.deslandes 建议的 00:00:00)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM