73

How do you round up/ round down a momentjs moment to nearest minute?

I have checked the docs, but there doesn't appear to be a method for this.

Note that I do not want a string rounded to the nearest minute, I want a moment returned (or modified in place, either is fine). I prefer not to have to convert to a string, and the convert back too.

Thanks.


As requested, here is some code:

var now = new moment(new Date());

if (now.seconds() > 0) {
    now.add('minutes', -1);
}

now.seconds(0);

as you can see, I have managed to manually round down the moment here, but it seems rather hacky. Just after a more elegant way of accomplishing this.

4

12 回答 12

110

要四舍五入,您需要增加一分钟,然后将其向下舍入。要四舍五入,只需使用该startOf方法。

请注意使用三元运算符来检查时间是否应该四舍五入(例如,点上的 13:00:00 不需要四舍五入)。

向上/向下舍入到最接近的分钟

var m = moment('2017-02-17 12:01:01');
var roundDown = m.startOf('minute');
console.log(roundDown.toString()); // outputs Tue Feb 17 2017 12:01:00 GMT+0000

var m = moment('2017-02-17 12:01:01');
var roundUp = m.second() || m.millisecond() ? m.add(1, 'minute').startOf('minute') : m.startOf('minute');
console.log(roundUp.toString());  // outputs Tue Feb 17 2017 12:02:00 GMT+0000

向上/向下舍入到最接近的小时

var m = moment('2017-02-17 12:59:59');
var roundDown = m.startOf('hour');
console.log(roundDown.toString()); // outputs Tue Feb 17 2017 12:00:00 GMT+0000

var m = moment('2017-02-17 12:59:59');
var roundUp = m.minute() || m.second() || m.millisecond() ? m.add(1, 'hour').startOf('hour') : m.startOf('hour');
console.log(roundUp.toString());  // outputs Tue Feb 17 2017 13:00:00 GMT+0000
于 2015-02-17T12:21:56.890 回答
26

部分答案:

向下舍入到最接近的时刻分钟:

var m = moment();
m.startOf('minute');

但是,四舍五入的等价物endOf, 并不能完全给出预期的结果。

于 2013-07-17T05:15:25.733 回答
21

可以通过添加半小时然后运行.startOf('hour')来实现四舍五入到最接近的小时。这对于任何时间测量都是一样的。

var now = moment();
// -> Wed Sep 30 2015 11:01:00
now.add(30, 'minutes').startOf('hour'); // -> Wed Sep 30 2015 11:31:00
// -> Wed Sep 30 2015 11:00:00

var now = moment();
// -> Wed Sep 30 2015 11:31:00
now.add(30, 'minutes').startOf('hour'); // -> Wed Sep 30 2015 12:01:00
// -> Wed Sep 30 2015 12:00:00
于 2017-03-14T12:03:18.800 回答
13

roundTo功能可以使其成为未来的版本。

例子:

moment().roundTo('minute', 15); // output: 12:45
moment().roundTo('minute', 15, 'down'); // output: 12:30
于 2014-07-24T04:39:18.687 回答
11

四舍五入

简单的。正如许多其他人所说,只需使用Moment.startOf

var roundDown = moment('2015-02-17 12:59:59').startOf('hour');
roundDown.format('HH:mm:SS'); // 12:00:00

重要的是,这也可以按预期工作:

var roundDown = moment('2015-02-17 12:00:00').startOf('hour');
roundDown.format('HH:mm:SS'); // 12:00:00

围捕

有点棘手,如果我们想用适当的上限函数四舍五入:例如,当按小时四舍五入时,我们想12:00:00四舍五入到12:00:00.

这不起作用

var roundUp = moment('2015-02-17 12:00:00').add(1, 'hour').startOf('hour');
roundUp.format('HH:mm:SS'); // ERROR: 13:00:00

解决方案

function roundUp(momentObj, roundBy){
  return momentObj.add(1, roundBy).startOf(roundBy);
}


var caseA = moment('2015-02-17 12:00:00');
roundUp(caseA, 'minute').format('HH:mm:SS'); // 12:00:00

var caseB = moment('2015-02-17 12:00:00.001');
roundUp(caseB, 'minute').format('HH:mm:SS'); // 12:01:00

var caseC = moment('2015-02-17 12:00:59');
roundUp(caseC, 'minute').format('HH:mm:SS'); // 12:01:00
于 2016-06-07T17:40:54.727 回答
7

更准确的答案:

t.add(30, 'seconds').startOf('minute')

案例 1:如果秒 < 30 则向下舍入

t = moment(); //12:00:05
t.add(30, 'seconds').startOf('minute') //12:00:00

案例 2:如果秒数 >= 30 则四舍五入

t = moment(); //12:00:33
t.add(30, 'seconds').startOf('minute') //12:01:00
于 2017-08-05T05:13:14.090 回答
4

我正在寻找同样的问题并找到了更好的解决方案:使用diff()函数中的第三个参数:

moment("2019-05-02 17:10:20").diff("2019-05-02 17:09:30","minutes",true)

通过将第三个参数设置为 true,您可以获得原始值作为响应,您可以使用自己四舍五入Math.round()

见 JSFiddle: https ://jsfiddle.net/2wqs4o0v/3/

于 2019-05-02T15:15:25.147 回答
2

这个解决方案对我有用;

function round_up_to_nearest_hour(date = new Date()) {
   return moment(date).add(59, 'minutes').startOf('hour').toDate();
}
于 2018-08-09T09:55:57.040 回答
1

只是另一种可能性:

const now = moment();
// -> Wed Sep 30 2015 11:57:20 GMT+0200 (CEST)
now.add(1, 'm').startOf('minute');
// -> Wed Sep 30 2015 11:58:00 GMT+0200 (CEST)
于 2015-09-30T09:59:02.133 回答
0

一个更好(和简单)的方法将是 -

moment('2021-07-22 14:56:58')
        .startOf('hour')
        .add(1, 'hour')
        // optional: .format('MMMM D, YYYY (h:mm A)')
//output: July 22, 2021 (3:00 PM)
于 2021-07-05T19:33:42.620 回答
0

四舍五入到下一个小时

一个简单的方法是 -

moment('2021-07-22 14:56:58')
        .startOf('hour')
        .add(1, 'hour')
        // optional: .format('MMMM D, YYYY (h:mm A)')
//output: July 22, 2021 (3:00 PM)
于 2021-07-05T19:35:18.360 回答
0

迄今为止最简单的解决方案:

function floor(time, floorBy = 'minute') {
  return time.startOf(floorBy);
}

function ceil(time, ceilBy = 'minute') {
  return time.subtract(1, 'millisecond').add(1, ceilBy).startOf(ceilBy);
}

// The solution is above. The code below is an optional test:

console.log(
  floor(moment('2019-01-01 12:00:00.000')).format('H:mm:ss.SSS') === '12:00:00.000',
   ceil(moment('2019-01-01 12:00:00.000')).format('H:mm:ss.SSS') === '12:00:00.000',
  floor(moment('2019-01-01 12:00:00.001')).format('H:mm:ss.SSS') === '12:00:00.000',
   ceil(moment('2019-01-01 12:00:00.001')).format('H:mm:ss.SSS') === '12:01:00.000',
  floor(moment('2019-01-01 12:15:16.876'), 'hour'  ).format('H:mm:ss.SSS') === '12:00:00.000',
   ceil(moment('2019-01-01 12:15:16.876'), 'hour'  ).format('H:mm:ss.SSS') === '13:00:00.000',
  floor(moment('2019-01-01 12:59:59.999'), 'second').format('H:mm:ss.SSS') === '12:59:59.000',
   ceil(moment('2019-01-01 12:59:59.999'), 'second').format('H:mm:ss.SSS') === '13:00:00.000',
  floor(moment('2019-01-01 12:00:00.025'), 'ms'    ).format('H:mm:ss.SSS') === '12:00:00.025',
   ceil(moment('2019-01-01 12:00:00.025'), 'ms'    ).format('H:mm:ss.SSS') === '12:00:00.025'
);
<script src="//cdn.jsdelivr.net/npm/moment@2.24.0/min/moment.min.js"></script>

于 2019-10-05T11:47:57.103 回答