javascript 向上/向下舍入一个 momentjs 时刻到最近的分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17691202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
round up/ round down a momentjs moment to nearest minute
提问by bguiz
How do you round up/ round down a momentjsmoment 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.
请注意,我不想要一个四舍五入到最接近的分钟的字符串,我想要一个moment
返回的(或就地修改,都可以)。我宁愿不必转换为字符串,也不必转换回来。
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.
正如你所看到的,我已经设法在这里手动舍入了这一刻,但它似乎相当笨拙。就在以更优雅的方式完成此操作之后。
回答by Liyali
To round up, you need to add a minute and then round it down. To round down, just use the startOf
method.
要四舍五入,您需要添加一分钟,然后将其四舍五入。要四舍五入,只需使用该startOf
方法。
Note the use of a ternary operator to check if the time should be rounded (for instance, 13:00:00 on the dot doesn't need to be rounded).
请注意使用三元运算符检查时间是否应该四舍五入(例如,点上的 13:00:00 不需要四舍五入)。
Round up/down to the nearest minute
向上/向下舍入到最近的分钟
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
Round up/down to the nearest hour
向上/向下舍入到最接近的小时
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
回答by bguiz
Partial answer:
部分答案:
To round down to nearest moment minute:
向下舍入到最近的时刻分钟:
var m = moment();
m.startOf('minute');
However, the equivalent for rounding up, endOf
, doesn't quite give the expected result.
但是,四舍五入的等价物endOf
, 并不能完全给出预期的结果。
回答by martinedwards
Rounding to the nearest hour can be achieved by adding half an hour and then run .startOf('hour'). This is the same for any time measurement.
通过添加半小时然后运行 .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
回答by Tri Q Tran
The roundTo featurecould make it into a future release.
该roundTo功能可以使之成为未来的版本。
Examples:
例子:
moment().roundTo('minute', 15); // output: 12:45
moment().roundTo('minute', 15, 'down'); // output: 12:30
回答by remeika
Rounding Down
四舍五入
Easy. As stated by many others, just use Moment.startOf:
简单。正如许多其他人所说,只需使用Moment.startOf:
var roundDown = moment('2015-02-17 12:59:59').startOf('hour');
roundDown.format('HH:mm:SS'); // 12:00:00
Importantly, this also works as expected:
重要的是,这也按预期工作:
var roundDown = moment('2015-02-17 12:00:00').startOf('hour');
roundDown.format('HH:mm:SS'); // 12:00:00
Rounding Up
围捕
Slightly trickier, if we want to round up with a proper ceiling function: for example, when rounding up by hour, we want 12:00:00
to round up to 12:00:00
.
稍微复杂一点,如果我们想用适当的天花板函数四舍五入:例如,当按小时四舍五入时,我们想要12:00:00
四舍五入到12:00:00
。
This does not work
这不起作用
var roundUp = moment('2015-02-17 12:00:00').add(1, 'hour').startOf('hour');
roundUp.format('HH:mm:SS'); // ERROR: 13:00:00
Solution
解决方案
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
回答by Vivek RC
A more precise answer:
更准确的答案:
t.add(30, 'seconds').startOf('minute')
Case1: Rounding down if seconds < 30
案例 1:如果秒数 < 30,则向下舍入
t = moment(); //12:00:05
t.add(30, 'seconds').startOf('minute') //12:00:00
Case2: Rounding up if seconds >= 30
案例 2:如果秒数 >= 30,则向上舍入
t = moment(); //12:00:33
t.add(30, 'seconds').startOf('minute') //12:01:00
回答by Sjoerd Loeve
I was searching for this same question and found a better solution:
Use the third parameter in diff()
function:
我正在寻找同样的问题,并找到了一个更好的解决方案:在diff()
函数中使用第三个参数:
moment("2019-05-02 17:10:20").diff("2019-05-02 17:09:30","minutes",true)
moment("2019-05-02 17:10:20").diff("2019-05-02 17:09:30","minutes",true)
By setting third parameter to true, you get the raw value as response that you can round by yourself using Math.round()
通过将第三个参数设置为 true,您可以获得原始值作为响应,您可以使用自己舍入 Math.round()
See JSFiddle: https://jsfiddle.net/2wqs4o0v/3/
见JSFiddle:https://jsfiddle.net/2wqs4o0v/3/
回答by Mokky Miah
This solution worked for me;
这个解决方案对我有用;
function round_up_to_nearest_hour(date = new Date()) {
return moment(date).add(59, 'minutes').startOf('hour').toDate();
}
回答by Manu
Just another possibility:
只是另一种可能:
var 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)
回答by Finesse
The simplest solution so far:
迄今为止最简单的解决方案:
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/[email protected]/min/moment.min.js"></script>