向上/向下舍入一个 momentjs 时刻到最近的分钟

编程入门 行业动态 更新时间:2024-10-24 20:18:02
本文介绍了向上/向下舍入一个 momentjs 时刻到最近的分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何将 momentjs 时刻向上/向下舍入到最接近的分钟?

我已经检查了 docs,但似乎没有针对此的方法.>

请注意,我不希望将字符串四舍五入到最接近的分钟,我希望返回一个 moment(或就地修改,两者都可以).我宁愿不必转换为字符串,也不必转换回来.

谢谢.

根据要求,这里有一些代码:

var now = new moment(new Date());如果(现在.秒()> 0){now.add('分钟', -1);}现在.秒(0);

如您所见,我已经设法手动舍入了此处的时间,但这似乎相当笨拙.就在以更优雅的方式完成此操作之后.

解决方案

要凑整,您需要添加一分钟然后 四舍五入.要向下舍入,只需使用 startOf 方法.

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

四舍五入到最接近的分钟

var m = moment('2017-02-17 12:01:01');var roundDown = m.startOf('minute');console.log(roundDown.toString());//输出 2017 年 2 月 17 日星期二 12:01:00 GMT+0000var m = moment('2017-02-17 12:01:01');var roundUp = m.second() ||毫秒()?m.add(1, 'minute').startOf('minute') : m.startOf('minute');console.log(roundUp.toString());//输出 2017 年 2 月 17 日星期二 12:02:00 GMT+0000

四舍五入到最接近的小时

var m = moment('2017-02-17 12:59:59');var roundDown = m.startOf('小时');console.log(roundDown.toString());//输出 2017 年 2 月 17 日星期二 12:00:00 GMT+0000var m = moment('2017-02-17 12:59:59');var roundUp = m.minute() ||m.second() ||毫秒()?m.add(1, 'hour').startOf('hour') : m.startOf('hour');console.log(roundUp.toString());//输出 2017 年 2 月 17 日星期二 13:00:00 GMT+0000

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.

解决方案

To round up, you need to add a minute and then round it down. To round down, just use the startOf method.

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).

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

更多推荐

向上/向下舍入一个 momentjs 时刻到最近的分钟

本文发布于:2023-10-16 05:27:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时刻   momentjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!