在javascript中将Date()舍入到最近的5分钟 [英] Round a Date() to the nearest 5 minutes in javascript

查看:115
本文介绍了在javascript中将Date()舍入到最近的5分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Date()实例,我如何将时间精确到最近的五分钟?

Using a Date() instance, how might I round a time to the nearest five minutes?

例如:如果是下午4:47将时间设置为下午4:45

For example: if it's 4:47 p.m. it'll set the time to 4:45 p.m.

推荐答案

如果你已经有一个那就很简单了日期对象:

That's pretty simple if you already have a Date object:

var coeff = 1000 * 60 * 5;
var date = new Date();  //or use any other date
var rounded = new Date(Math.round(date.getTime() / coeff) * coeff)

这篇关于在javascript中将Date()舍入到最近的5分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆