everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
24 lines (23 loc) • 717 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.roundDateToNearest = void 0;
/**
* Rounds date/time to nearest N minutes or seconds.
* @author @dailker
* @param {Date} date
* @param {object} opts - {minutes?: number, seconds?: number}
* @returns {Date}
*/
function roundDateToNearest(date, opts) {
const d = new Date(date);
if (opts.minutes) {
const ms = opts.minutes * 60 * 1000;
d.setTime(Math.round(d.getTime() / ms) * ms);
}
else if (opts.seconds) {
const ms = opts.seconds * 1000;
d.setTime(Math.round(d.getTime() / ms) * ms);
}
return d;
}
exports.roundDateToNearest = roundDateToNearest;