在javascript中将Date()舍入到最近的5分钟

时间:2012-05-28 19:10:30

标签: javascript

使用Date()实例,我怎样才能将时间缩短到最近的五分钟?

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

10 个答案:

答案 0 :(得分:173)

如果您已经拥有Date对象,那就非常简单了:

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

答案 1 :(得分:5)

舍入到最接近的x分钟

这是一个将日期对象舍入到最近的x分钟的方法,或者如果你没有给它任何日期,它将围绕当前时间。

let getRoundedDate = (minutes, d=new Date()) => {

  let ms = 1000 * 60 * minutes; // convert minutes to ms
  let roundedDate = new Date(Math.round(d.getTime() / ms) * ms);

  return roundedDate
}


// USAGE //

// Round existing date to 5 minutes
getRoundedDate(5, new Date()); // 2018-01-26T00:45:00.000Z

// Get current time rounded to 30 minutes
getRoundedDate(30); // 2018-01-26T00:30:00.000Z

答案 2 :(得分:4)

我知道回答有点迟,但也许可以帮助别人。如果您按以下时间记录会议记录

new Date().getMinutes()

你可以花费最后5分钟

new Date().getMinutes() - (new Date().getMinutes()%5)

答案 3 :(得分:1)

借助ES6和部分功能,它可能很优雅:

const roundDownTo = roundTo => x => Math.floor(x / roundTo) * roundTo;
const roundUpTo = roundTo => x => Math.ceil(x / roundTo) * roundTo;
const roundDownTo5Minutes = roundDownTo(1000*60*5);

const ms = roundDownTo5Minutes(new Date())
console.log(new Date(ms)); // Wed Jun 05 2019 15:55:00 GMT+0200

答案 4 :(得分:1)

使用此方法使用纯JS获取下一个5分钟的循环

function calculateNextCycle(interval) {
    const timeStampCurrentOrOldDate = Date.now();
    const timeStampStartOfDay = new Date().setHours(0, 0, 0, 0);
    const timeDiff = timeStampCurrentOrOldDate - timeStampStartOfDay;
    const mod = Math.ceil(timeDiff / interval);
    return new Date(timeStampStartOfDay + (mod * interval));
}

console.log(calculateNextCycle(5 * 60 * 1000)); // pass in milliseconds

答案 5 :(得分:1)

Date-fns 现在有一个函数可以在日期上舍入分钟。见https://date-fns.org/v2.21.3/docs/roundToNearestMinutes

const roundToNearestMinutes = require('date-fns/roundToNearestMinutes')
console.log(roundToNearestMinutes(new Date(), {nearestTo: 5}));
// e.g. 2021-05-19T22:45:00.000Z

答案 6 :(得分:0)

最近发现了一种非常有效的方法来将日期四舍五入到时间范围

简而言之:

// minutes
var tf = 15; // 5,10,13,15, 60, - what ever you want
var dt = DateTime.UtcNow;
var minues = dt.TimeOfDay.TotalMinutes; // use TotalMinutes, TotalSecibds, TotalMillisecons  and etc
var roundedMinutes = (minues - (minues%tf));
var roundedDate = dt.Date.AddMinutes(a);

我在LINQPad中进行了一些测试

// minutes
var tf = 15; // 5,10,13,15, 60, - what ever you want
var dt = DateTime.UtcNow;
var minues = dt.TimeOfDay.TotalMinutes;
dt.Dump();
minues.Dump();
(ms%tf).Dump();
var a = (minues - (minues%tf));
a.Dump();
dt.Date.AddMinutes(a).Dump();

输出:

13.07.2018 7:43:58 - current date
463,981443103333 - total mins
13,9814431033333 - rest
450 - rounded minutes value
13.07.2018 7:30:00 - rounded date

答案 7 :(得分:0)

有一个Firebase Realtime @qc/date-round可以使用。假设您有一个Date实例要四舍五入

import { round } from '@qc/date-round'

const dateIn = ...; // The date to be rounded
const interval = 5 * 60 * 1000; // 5 minutes
const dateOut = round(dateIn, interval)

然后您可以使用NPM package设置日期格式

import format from 'date-fns/format';

console.log(format(dateOut, 'HH:mm')) // 24-hr
console.log(format(dateOut, 'hh:mm a')) // 12-hr

答案 8 :(得分:0)

效率可能较低,但这是另一种选择:

debug('Current timestamp:', timestamp);

timestamp.setMilliseconds(0);
timestamp.setSeconds(0);
timestamp.setMinutes(Math.round(timestamp.getMinutes() / 5) * 5);

debug('Rounded timestamp:', timestamp);
Current timestamp: 2019-10-22T09:47:17.989Z
Rounded timestamp: 2019-10-22T09:45:00.000Z

答案 9 :(得分:0)

单行解决方案(向上或向下舍入):

const fixedTime = (isRoundUp ? Math.ceil : Math.floor)(time / 60_000 / minutesRange)) * 60_000 * minutesRange;

// minutesRange: number -> 1, 5, 15, 30, etc // minutes
// isRoundUp: boolean
// time: number // millis