Skip to main content

latest

Utility for doing math on strings in the format "hh:mm", such as adding, subtracting, intersection testing and modulo

This package works with Bun, Node.js, Deno, BrowsersIt is unknown whether this package works with Cloudflare Workers
JSR Score
94%
Published
2 weeks ago (0.1.2)

hhmm library logo

An easy and serialization-friendly way to handle HH:MM calculations

Here are some motivating examples for this library.

// Using DateTime
const 

if you'd like to check equality and inequality

This library makes it easy to do the following:

  • Adding and subtracting times
// Using class instances
const add1 = new HHMM("08:00").add("01:00")
    // ^? = HHMM { minute = 540 }
const sub1 = new HHMM("08:00").subtract("00:15")
    // ^? = HHMM { minute = 465 }

// Using the HM symbol property
const add2 = "08:00"[HM].add("09:00")
const sub2 = "08:00"[HM].subtract("09:00")
  • Divide
// Using class instances
const newTime = new HHMM("08:00").subtract("09:00")

// Using the HM symbol property
const newTime = "08:00"[HM].subtract("09:00")

Symbol property mode

To start using the HM symbol property, the String interface should be augmented to get proper typescript types.

// global.d.ts
declare global {
    interface String {
        [HM]: HHMM;
    }
}

In addition, you must import @revosw/hhmm/symbol. Importing this file has the side effect of adding the HM symbol as a property on the String.prototype object.

import "@revosw/hhmm/symbol"

You will suffer if you try to

It is tiresome having to create class instances all the time for the simplest things.

const from = new HHMM("08:00")
const to = new HHMM("09:00")

if (from.lt(to)) {
    // from < to
}

if (new HHMM("08:00") < new HHMM("09:00"))

Problems with structuredClone()

Since class instances

Problems with React

Problems with TanStack Query

The query cache does not like

While it's possible to use the

Add Package

deno add @revosw/hhmm

Import symbol

import * as mod from "@revosw/hhmm";

Add Package

npx jsr add @revosw/hhmm

Import symbol

import * as mod from "@revosw/hhmm";

Add Package

yarn dlx jsr add @revosw/hhmm

Import symbol

import * as mod from "@revosw/hhmm";

Add Package

pnpm dlx jsr add @revosw/hhmm

Import symbol

import * as mod from "@revosw/hhmm";

Add Package

bunx jsr add @revosw/hhmm

Import symbol

import * as mod from "@revosw/hhmm";