datetime-round
Version:
Rounding off datetime to perfect time interval
22 lines (17 loc) • 558 B
JavaScript
const moment = require('moment');
/**
*
* @param {*} date input date to round off the time to
* @param {*} duration interval to round off the value to fixed time
* @param {*} method type of round, ciel / floor
*
* Returns moment object
*/
const round = (date, interval, intervalType, method) => {
if (typeof date === 'string') {
date = moment(date);
}
const momentDuration = moment.duration(parseInt(interval), intervalType);
return moment(Math[method]((+date) / (+momentDuration)) * (+momentDuration));
};
module.exports = round;