Skip to content

cityssm/node-to-millis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

To Millis

npm (scoped) Maintainability DeepSource codecov Coverage Testing

Increases the readability of millisecond (and second) values in JavaScript.

Handy when used with functions like setTimeout() and setInterval() to provide easy-to-read durations, or when adding to times represented in milliseconds.

Installation

npm install @cityssm/to-millis

Basic Usage

import toMillis from '@cityssm/to-millis'

/*
 * With toMillis 👍
 */

setInterval(dailyTask, toMillis(1, 'day'))

const twoHoursFromNow = Date.now() + toMillis(2, 'hours')

/*
 * Without toMillis,
 * you may expand the calculation to make the amount more readable. 😕
 */

setInterval(dailyTask, 1 * 24 * 60 * 60 * 1000)

const twoHoursFromNow = Date.now() + 2 * 60 * 60 * 1000

More Exports

  • toSeconds(durationAmount, durationUnit)

Functions without units:

  • daysToMillis(durationDays)
  • hoursToMillis(durationHours)
  • minutesToMillis(durationMinutes)
  • secondsToMillis(durationSeconds)
  • daysToSeconds(durationDays)
  • hoursToSeconds(durationHours)
  • minutesToSeconds(durationMinutes)

Arrays of acceptable units:

  • durationUnitsDays
  • durationUnitsHours
  • durationUnitsMinutes
  • durationUnitsSeconds
  • durationUnitsMilliseconds

Multipliers used for calculations:

  • millisecondsInOneDay
  • millisecondsInOneHour
  • millisecondsInOneMinute
  • millisecondsInOneSecond
  • secondsInOneDay
  • secondsInOneHour
  • secondsInOneMinute