1.0.0 • Published 4 years ago

round-time v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

Round Time

npm version CircleCI Status MIT license

A small library of functions for rounding minutes, seconds, and milliseconds by intervals.

  • Accepts and returns plain JavaScript Date objects.
  • All functions will return a new Date object with the rounding applied.

Installation

npm install round-time;

Usage

const round = require("round-time");

const roundedMinutes = round.minutesNearest(new Date(), 10);

Rounding Functions

Replace "unit" with minutes, seconds, or milliseconds.

unitUp: Rounds up to the nearest multiple of the provided interval.

unitDown: Rounds down to the nearest multiple of the provided interval.

unitNearest: Rounds to the nearest multiple of the provided interval.

Compete list of functions: minutesUp, minutesDown, minutesNearest, secondsUp, secondsDown, secondsNearest, millisecondsUp, millisecondsDown, millisecondsNearest.

Arguments

All functions take 2 arguments. A date object and an optional increment number (if no increment is provided, it will use the default value of 1). The following example would round to the nearest 15 minutes: minutesNearest(new Date(), 15)

Time Increments

  • Minute and second increments must be factors of 60.
  • Millisecond increments must be factors of 1000.
  • Minutes and seconds accept decimal increments (2.5, 7.5, etc).