Time utility
Go to file
2024-03-04 11:25:17 +01:00
.github funding 2020-05-26 10:38:30 +02:00
src update deps and add esm support 2022-07-16 17:29:22 +02:00
test testing 2022-07-16 17:34:20 +02:00
.gitignore update deps and add esm support 2022-07-16 17:29:22 +02:00
LICENSE Create LICENSE 2020-03-20 11:22:56 +01:00
package.json update deps 2024-03-04 11:25:17 +01:00
pnpm-lock.yaml update deps 2024-03-04 11:25:17 +01:00
README.md Update README.md 2021-03-15 18:46:33 +01:00
tsconfig.json update deps 2024-03-04 11:25:17 +01:00

uhrwerk 🕰

dependencies downloads badge types badge version badge minzip size badge

Minimal time duration utility. Replacement for MomentJS Durations. If you are looking into the time component of MomentJS check out this awesome library dayjs.

📦 It's tiny: 1.6kB vs moment js 231.7kb

Typescript typings included

Quickstart 🚀

// Whatever import you prefer
// const { Duration } = require('uhrwerk')
import { Duration } from 'uhrwerk'

const d = new Duration(10, 'days')
d.subtract(1, 'week')
d.add(5, 'minutes')

d.humanize()    // '3 days'
d.minutes()     // 5
d.asMinute()    // 4325

d.subtract(3, 'days')
d.humanize()    // 'a few minutes'

Reference 📒

new Duration(amount, interval)

  • amount: number
  • interval:
    • millisecond, milliseconds, ms
    • second, seconds, s
    • minute, minutes, m
    • hour, hours, h
    • day, days, d
    • week, weeks, w
    • year, years, y
Examples
const a = new Duration(1, 'day')
const b = new Duration(2, 'days')
const c = new Duration(0.5, 'year')
const d = new Duration (Date.now(), 'ms')

.add(amount, interval)

Adds a specified amount to an existing duration

Example
const a = new Duration(1, 'day')
a.add(12, 'hours')
a.asHour() // 36

.subtract(amount, interval)

Subtracts a specified amount to an existing duration

Example
const a = new Duration(1, 'day')
a.subtract(12, 'hours')
a.asHour() // 12

Getters

Gets the amount of time interval, not the total time

  • .milliseconds()
  • .seconds()
  • .minutes()
  • .hours()
  • .days()
  • .weeks()
  • .years()
Example
const a = new Duration(1, 'day')
a.days() // 1
a.add(5, 'minutes')
a.days() // 1
a.add(1, 'year')
a.days() // 1
a.add(24, 'hours')
a.days() // 2

As interval

Calculates the time duration as a time interval.

  • .asMilliseconds()
  • .asSeconds()
  • .asMinutes()
  • .asHours()
  • .asDays()
  • .asWeeks()
  • .asYears()
Example
const a = new Duration(1, 'day')
a.asHours() // 24

.humanize()

This functions takes a duration and tries to make a human readable version out of it.

Example
const a = new Duration(4, 'seconds')
a.humanize() // 'a moment'
a.add(5, 'minutes')
a.humanize() // 'a few minutes'
Own rules / i18n

If you want to pass a different humanize function you can. The order of the array is important. The first match will return, like in a standard server router. The first argument is a function that takes the duration and returns a boolean. The second takes also matched duration and returns a string for the user.

Example
const humanizer = [
	[d => d.days() > 1, d => `${d.days()} days`],
	[d => d.days() > 0, d => `1 day`],
	[() => true, () => 'catch all, below 1 day'],
]

const a = new Duration(2, 'days')
a.humanize(humanizer) // '2 days'
a.subtract(1, 'day')
a.humanize(humanizer) // '1 day'
a.subtract(12, 'hours')
a.humanize(humanizer) // 'catch all, below 1 day'